Skip to content

Commit 5cec383

Browse files
committed
Sync 2017-08-06
1 parent 5f55fe3 commit 5cec383

13 files changed

+179
-49
lines changed

cpp_utils/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/docs/
2+
/Arduino/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=ESP32_BLE
2+
version=1.0
3+
author=Neil Kolban <[email protected]>
4+
maintainer=Neil Kolban <[email protected]>
5+
sentence=BLE functions for ESP32
6+
paragraph=BLE functions for ESP32
7+
category=Communication
8+
url=http://example.com/
9+
architectures=esp32
10+
includes=BLE.h BLEUtils.h BLEScan.h BLEAdvertisedDevice.h

cpp_utils/BLE.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
#include "sdkconfig.h"
88
#if defined(CONFIG_BT_ENABLED)
9+
#include <esp_err.h>
910
#include <nvs_flash.h>
1011
#include <freertos/FreeRTOS.h>
1112
#include <freertos/event_groups.h>

cpp_utils/BLECharacteristic.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static const char* LOG_TAG = "BLECharacteristic";
3131
BLECharacteristic::BLECharacteristic(BLEUUID uuid, uint32_t properties) {
3232
m_bleUUID = uuid;
3333
m_handle = NULL_HANDLE;
34-
m_properties = 0;
34+
m_properties = (esp_gatt_char_prop_t)0;
3535
m_pCallbacks = nullptr;
3636

3737
setBroadcastProperty((properties & PROPERTY_BROADCAST) !=0);
@@ -411,13 +411,18 @@ void BLECharacteristic::handleGATTServerEvent(
411411
*/
412412
void BLECharacteristic::indicate() {
413413

414-
char *pHexData = BLEUtils::buildHexData(nullptr, (uint8_t*)m_value.getValue().data(), m_value.getValue().length());
415-
ESP_LOGD(LOG_TAG, ">> indicate: length: %d, data: [%s]", m_value.getValue().length(), pHexData );
416-
free(pHexData);
414+
ESP_LOGD(LOG_TAG, ">> indicate: length: %d", m_value.getValue().length());
417415

418416
assert(getService() != nullptr);
419417
assert(getService()->getServer() != nullptr);
420418

419+
GeneralUtils::hexDump((uint8_t*)m_value.getValue().data(), m_value.getValue().length());
420+
421+
if (getService()->getServer()->getConnectedCount() == 0) {
422+
ESP_LOGD(LOG_TAG, "<< indicate: No connected clients.");
423+
return;
424+
}
425+
421426
// Test to see if we have a 0x2902 descriptor. If we do, then check to see if indications are enabled
422427
// and, if not, prevent the indication.
423428

@@ -460,15 +465,20 @@ void BLECharacteristic::indicate() {
460465
* @return N/A.
461466
*/
462467
void BLECharacteristic::notify() {
463-
464-
char *pHexData = BLEUtils::buildHexData(nullptr, (uint8_t*)m_value.getValue().data(), m_value.getValue().length());
465-
ESP_LOGD(LOG_TAG, ">> notify: length: %d, data: [%s]", m_value.getValue().length(), pHexData );
466-
free(pHexData);
468+
ESP_LOGD(LOG_TAG, ">> notify: length: %d", m_value.getValue().length());
467469

468470

469471
assert(getService() != nullptr);
470472
assert(getService()->getServer() != nullptr);
471473

474+
475+
GeneralUtils::hexDump((uint8_t*)m_value.getValue().data(), m_value.getValue().length());
476+
477+
if (getService()->getServer()->getConnectedCount() == 0) {
478+
ESP_LOGD(LOG_TAG, "<< notify: No connected clients.");
479+
return;
480+
}
481+
472482
// Test to see if we have a 0x2902 descriptor. If we do, then check to see if notification is enabled
473483
// and, if not, prevent the notification.
474484

@@ -510,9 +520,9 @@ void BLECharacteristic::notify() {
510520
void BLECharacteristic::setBroadcastProperty(bool value) {
511521
//ESP_LOGD(LOG_TAG, "setBroadcastProperty(%d)", value);
512522
if (value) {
513-
m_properties |= ESP_GATT_CHAR_PROP_BIT_BROADCAST;
523+
m_properties = (esp_gatt_char_prop_t)(m_properties | ESP_GATT_CHAR_PROP_BIT_BROADCAST);
514524
} else {
515-
m_properties &= ~ESP_GATT_CHAR_PROP_BIT_BROADCAST;
525+
m_properties = (esp_gatt_char_prop_t)(m_properties & ~ESP_GATT_CHAR_PROP_BIT_BROADCAST);
516526
}
517527
} // setBroadcastProperty
518528

@@ -550,9 +560,9 @@ void BLECharacteristic::setHandle(uint16_t handle) {
550560
void BLECharacteristic::setIndicateProperty(bool value) {
551561
//ESP_LOGD(LOG_TAG, "setIndicateProperty(%d)", value);
552562
if (value) {
553-
m_properties |= ESP_GATT_CHAR_PROP_BIT_INDICATE;
563+
m_properties = (esp_gatt_char_prop_t)(m_properties | ESP_GATT_CHAR_PROP_BIT_INDICATE);
554564
} else {
555-
m_properties &= ~ESP_GATT_CHAR_PROP_BIT_INDICATE;
565+
m_properties = (esp_gatt_char_prop_t)(m_properties & ~ESP_GATT_CHAR_PROP_BIT_INDICATE);
556566
}
557567
} // setIndicateProperty
558568

@@ -564,9 +574,9 @@ void BLECharacteristic::setIndicateProperty(bool value) {
564574
void BLECharacteristic::setNotifyProperty(bool value) {
565575
//ESP_LOGD(LOG_TAG, "setNotifyProperty(%d)", value);
566576
if (value) {
567-
m_properties |= ESP_GATT_CHAR_PROP_BIT_NOTIFY;
577+
m_properties = (esp_gatt_char_prop_t)(m_properties | ESP_GATT_CHAR_PROP_BIT_NOTIFY);
568578
} else {
569-
m_properties &= ~ESP_GATT_CHAR_PROP_BIT_NOTIFY;
579+
m_properties = (esp_gatt_char_prop_t)(m_properties & ~ESP_GATT_CHAR_PROP_BIT_NOTIFY);
570580
}
571581
} // setNotifyProperty
572582

@@ -578,9 +588,9 @@ void BLECharacteristic::setNotifyProperty(bool value) {
578588
void BLECharacteristic::setReadProperty(bool value) {
579589
//ESP_LOGD(LOG_TAG, "setReadProperty(%d)", value);
580590
if (value) {
581-
m_properties |= ESP_GATT_CHAR_PROP_BIT_READ;
591+
m_properties = (esp_gatt_char_prop_t)(m_properties | ESP_GATT_CHAR_PROP_BIT_READ);
582592
} else {
583-
m_properties &= ~ESP_GATT_CHAR_PROP_BIT_READ;
593+
m_properties = (esp_gatt_char_prop_t)(m_properties & ~ESP_GATT_CHAR_PROP_BIT_READ);
584594
}
585595
} // setReadProperty
586596

@@ -622,9 +632,9 @@ void BLECharacteristic::setValue(std::string value) {
622632
void BLECharacteristic::setWriteNoResponseProperty(bool value) {
623633
//ESP_LOGD(LOG_TAG, "setWriteNoResponseProperty(%d)", value);
624634
if (value) {
625-
m_properties |= ESP_GATT_CHAR_PROP_BIT_WRITE_NR;
635+
m_properties = (esp_gatt_char_prop_t)(m_properties | ESP_GATT_CHAR_PROP_BIT_WRITE_NR);
626636
} else {
627-
m_properties &= ~ESP_GATT_CHAR_PROP_BIT_WRITE_NR;
637+
m_properties = (esp_gatt_char_prop_t)(m_properties & ~ESP_GATT_CHAR_PROP_BIT_WRITE_NR);
628638
}
629639
} // setWriteNoResponseProperty
630640

@@ -636,9 +646,9 @@ void BLECharacteristic::setWriteNoResponseProperty(bool value) {
636646
void BLECharacteristic::setWriteProperty(bool value) {
637647
//ESP_LOGD(LOG_TAG, "setWriteProperty(%d)", value);
638648
if (value) {
639-
m_properties |= ESP_GATT_CHAR_PROP_BIT_WRITE;
649+
m_properties = (esp_gatt_char_prop_t)(m_properties | ESP_GATT_CHAR_PROP_BIT_WRITE);
640650
} else {
641-
m_properties &= ~ESP_GATT_CHAR_PROP_BIT_WRITE;
651+
m_properties = (esp_gatt_char_prop_t)(m_properties & ~ESP_GATT_CHAR_PROP_BIT_WRITE);
642652
}
643653
} // setWriteProperty
644654

cpp_utils/BLEDescriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void BLEDescriptor::executeCreate(BLECharacteristic* pCharacteristic) {
6363
esp_err_t errRc = ::esp_ble_gatts_add_char_descr(
6464
pCharacteristic->getService()->getHandle(),
6565
getUUID().getNative(),
66-
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
66+
(esp_gatt_perm_t)(ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE),
6767
&m_value,
6868
&control);
6969
if (errRc != ESP_OK) {

cpp_utils/BLEServer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static const char* LOG_TAG = "BLEServer";
3333
BLEServer::BLEServer() {
3434
m_appId = -1;
3535
m_gatts_if = -1;
36+
m_connectedCount = 0;
3637
m_connId = -1;
3738
BLE::m_bleServer = this;
3839
m_pServerCallbacks = nullptr;
@@ -90,6 +91,16 @@ uint16_t BLEServer::getConnId() {
9091
return m_connId;
9192
}
9293

94+
95+
/**
96+
* @brief Return the number of connected clients.
97+
* @return The number of connected clients.
98+
*/
99+
uint32_t BLEServer::getConnectedCount() {
100+
return m_connectedCount;
101+
} // getConnectedCount
102+
103+
93104
uint16_t BLEServer::getGattsIf() {
94105
return m_gatts_if;
95106
}
@@ -162,6 +173,7 @@ void BLEServer::handleGATTServerEvent(
162173
if (m_pServerCallbacks != nullptr) {
163174
m_pServerCallbacks->onConnect(this);
164175
}
176+
m_connectedCount++;
165177
break;
166178
} // ESP_GATTS_CONNECT_EVT
167179

@@ -232,6 +244,7 @@ void BLEServer::handleGATTServerEvent(
232244
// If we receive a disconnect event then invoke the callback for disconnects (if one is present).
233245
// we also want to start advertising again.
234246
case ESP_GATTS_DISCONNECT_EVT: {
247+
m_connectedCount--;
235248
if (m_pServerCallbacks != nullptr) {
236249
m_pServerCallbacks->onDisconnect(this);
237250
}

cpp_utils/BLEServer.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
#define COMPONENTS_CPP_UTILS_BLESERVER_H_
1010
#include "sdkconfig.h"
1111
#if defined(CONFIG_BT_ENABLED)
12+
#include <esp_gatts_api.h>
13+
1214
#include <string>
1315
#include <string.h>
14-
#include <esp_gatts_api.h>
15-
#include "FreeRTOS.h"
16+
1617
#include "BLEUUID.h"
1718
#include "BLEAdvertising.h"
1819
#include "BLECharacteristic.h"
1920
#include "BLEService.h"
21+
#include "FreeRTOS.h"
2022

2123
class BLEServerCallbacks;
2224

@@ -26,18 +28,19 @@ class BLEServerCallbacks;
2628
*/
2729
class BLEServiceMap {
2830
public:
29-
void setByUUID(BLEUUID uuid, BLEService* service);
30-
void setByHandle(uint16_t handle, BLEService* service);
31-
BLEService* getByUUID(BLEUUID uuid);
3231
BLEService* getByHandle(uint16_t handle);
33-
std::string toString();
34-
void handleGATTServerEvent(
32+
BLEService* getByUUID(BLEUUID uuid);
33+
void handleGATTServerEvent(
3534
esp_gatts_cb_event_t event,
3635
esp_gatt_if_t gatts_if,
37-
esp_ble_gatts_cb_param_t *param);
38-
private:
39-
std::map<std::string, BLEService*> m_uuidMap;
40-
std::map<uint16_t, BLEService*> m_handleMap;
36+
esp_ble_gatts_cb_param_t* param);
37+
void setByHandle(uint16_t handle, BLEService* service);
38+
void setByUUID(BLEUUID uuid, BLEService* service);
39+
std::string toString();
40+
41+
private:
42+
std::map<std::string, BLEService*> m_uuidMap;
43+
std::map<uint16_t, BLEService*> m_handleMap;
4144
};
4245

4346

@@ -49,6 +52,7 @@ class BLEServer {
4952
BLEServer();
5053

5154

55+
uint32_t getConnectedCount();
5256
BLEService* createService(BLEUUID uuid);
5357
BLEAdvertising* getAdvertising();
5458
void setCallbacks(BLEServerCallbacks *pCallbacks);
@@ -62,8 +66,9 @@ class BLEServer {
6266
esp_ble_adv_data_t m_adv_data;
6367
uint16_t m_appId;
6468
BLEAdvertising m_bleAdvertising;
65-
uint16_t m_gatts_if;
6669
uint16_t m_connId;
70+
uint32_t m_connectedCount;
71+
uint16_t m_gatts_if;
6772
FreeRTOS::Semaphore m_semaphoreRegisterAppEvt = FreeRTOS::Semaphore("RegisterAppEvt");
6873
FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
6974
BLEServiceMap m_serviceMap;

cpp_utils/BLEService.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,6 @@ void BLEService::start() {
118118
return;
119119
}
120120

121-
m_semaphoreStartEvt.take("start");
122-
esp_err_t errRc = ::esp_ble_gatts_start_service(m_handle);
123-
124-
if (errRc != ESP_OK) {
125-
ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_start_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
126-
return;
127-
}
128-
m_semaphoreStartEvt.wait("start");
129121

130122
BLECharacteristic *pCharacteristic = m_characteristicMap.getFirst();
131123

@@ -137,6 +129,15 @@ void BLEService::start() {
137129
}
138130
// Start each of the characteristics ... these are found in the m_characteristicMap.
139131

132+
m_semaphoreStartEvt.take("start");
133+
esp_err_t errRc = ::esp_ble_gatts_start_service(m_handle);
134+
135+
if (errRc != ESP_OK) {
136+
ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_start_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
137+
return;
138+
}
139+
m_semaphoreStartEvt.wait("start");
140+
140141
ESP_LOGD(LOG_TAG, "<< start()");
141142
} // start
142143

cpp_utils/GeneralUtils.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ void GeneralUtils::hexDump(uint8_t* pData, uint32_t length) {
265265
char ascii[80];
266266
char hex[80];
267267
char tempBuf[80];
268+
uint32_t lineNumber = 0;
269+
270+
ESP_LOGD(LOG_TAG, " 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ----------------");
268271
strcpy(ascii, "");
269272
strcpy(hex, "");
270273
uint32_t index=0;
@@ -279,17 +282,18 @@ void GeneralUtils::hexDump(uint8_t* pData, uint32_t length) {
279282
strcat(ascii, tempBuf);
280283
index++;
281284
if (index % 16 == 0) {
282-
ESP_LOGD(LOG_TAG, "%s %s", hex, ascii);
285+
ESP_LOGD(LOG_TAG, "%.4x %s %s", lineNumber*16, hex, ascii);
283286
strcpy(ascii, "");
284287
strcpy(hex, "");
288+
lineNumber++;
285289
}
286290
}
287291
if (index %16 != 0) {
288292
while(index % 16 != 0) {
289293
strcat(hex, " ");
290294
index++;
291295
}
292-
ESP_LOGD(LOG_TAG, "%s %s", hex, ascii);
296+
ESP_LOGD(LOG_TAG, "%.4x %s %s", lineNumber*16, hex, ascii);
293297
}
294298
} // hexDump
295299

cpp_utils/I2S.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static void i2s_conf_reset()
198198
}
199199
}
200200

201-
201+
/*
202202
static void IRAM_ATTR logI2SIntr() {
203203
std::ostringstream ss;
204204
ss << "IN_DONE: " << I2S0.int_raw.in_done;
@@ -208,7 +208,9 @@ static void IRAM_ATTR logI2SIntr() {
208208
ss << ", IN_SUC_EOF: " << I2S0.int_raw.in_suc_eof;
209209
ESP_EARLY_LOGV(LOG_TAG, "I2S Intr: %s", ss.str().c_str());
210210
}
211+
*/
211212

213+
/*
212214
static void IRAM_ATTR logDesc(lldesc_t* pDesc) {
213215
std::ostringstream ss;
214216
ss << "size: " << pDesc->size;
@@ -219,8 +221,8 @@ static void IRAM_ATTR logDesc(lldesc_t* pDesc) {
219221
ss << ", eof: " << pDesc->eof;
220222
ss << ", owner: " << pDesc->owner;
221223
ESP_EARLY_LOGV(LOG_TAG, "Desc: %s", ss.str().c_str());
222-
223224
}
225+
*/
224226

225227

226228
/**

0 commit comments

Comments
 (0)