Skip to content

Commit 810aeb8

Browse files
authored
Merge pull request #800 from chegewara/master
Few bugfixes and add functions Fix #796 , fix #786 fix #744 fix #751
2 parents ac94b34 + d141a91 commit 810aeb8

11 files changed

+61
-44
lines changed

cpp_utils/BLEAdvertisedDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class BLEAdvertisedDevice {
9595
int8_t m_txPower;
9696
std::string m_serviceData;
9797
BLEUUID m_serviceDataUUID;
98-
uint8_t* m_payload;
98+
uint8_t* m_payload = nullptr;
9999
size_t m_payloadLength = 0;
100100
esp_ble_addr_type_t m_addressType;
101101
};

cpp_utils/BLECharacteristic.cpp

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,24 @@ void BLECharacteristic::handleGATTServerEvent(
224224
// - uint8_t exec_write_flag - Either ESP_GATT_PREP_WRITE_EXEC or ESP_GATT_PREP_WRITE_CANCEL
225225
//
226226
case ESP_GATTS_EXEC_WRITE_EVT: {
227-
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
228-
m_value.commit();
229-
if (m_pCallbacks != nullptr) {
230-
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
227+
if(m_writeEvt){
228+
m_writeEvt = false;
229+
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
230+
m_value.commit();
231+
if (m_pCallbacks != nullptr) {
232+
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
233+
}
234+
} else {
235+
m_value.cancel();
236+
}
237+
// ???
238+
esp_err_t errRc = ::esp_ble_gatts_send_response(
239+
gatts_if,
240+
param->write.conn_id,
241+
param->write.trans_id, ESP_GATT_OK, nullptr);
242+
if (errRc != ESP_OK) {
243+
ESP_LOGE(LOG_TAG, "esp_ble_gatts_send_response: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
231244
}
232-
} else {
233-
m_value.cancel();
234-
}
235-
// ???
236-
esp_err_t errRc = ::esp_ble_gatts_send_response(
237-
gatts_if,
238-
param->write.conn_id,
239-
param->write.trans_id, ESP_GATT_OK, nullptr);
240-
if (errRc != ESP_OK) {
241-
ESP_LOGE(LOG_TAG, "esp_ble_gatts_send_response: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
242245
}
243246
break;
244247
} // ESP_GATTS_EXEC_WRITE_EVT
@@ -284,7 +287,11 @@ void BLECharacteristic::handleGATTServerEvent(
284287
if (param->write.handle == m_handle) {
285288
if (param->write.is_prep) {
286289
m_value.addPart(param->write.value, param->write.len);
290+
m_writeEvt = true;
287291
} else {
292+
if (m_pCallbacks != nullptr && param->write.is_prep != true) {
293+
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
294+
}
288295
setValue(param->write.value, param->write.len);
289296
}
290297

@@ -313,9 +320,6 @@ void BLECharacteristic::handleGATTServerEvent(
313320
}
314321
} // Response needed
315322

316-
if (m_pCallbacks != nullptr && param->write.is_prep != true) {
317-
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
318-
}
319323
} // Match on handles.
320324
break;
321325
} // ESP_GATTS_WRITE_EVT
@@ -384,6 +388,9 @@ void BLECharacteristic::handleGATTServerEvent(
384388
}
385389
} else { // read.is_long == false
386390

391+
if (m_pCallbacks != nullptr) { // If is.long is false then this is the first (or only) request to read data, so invoke the callback
392+
m_pCallbacks->onRead(this); // Invoke the read callback.
393+
}
387394
std::string value = m_value.getValue();
388395

389396
if (value.length() + 1 > maxOffset) {
@@ -399,9 +406,9 @@ void BLECharacteristic::handleGATTServerEvent(
399406
memcpy(rsp.attr_value.value, value.data(), rsp.attr_value.len);
400407
}
401408

402-
if (m_pCallbacks != nullptr) { // If is.long is false then this is the first (or only) request to read data, so invoke the callback
403-
m_pCallbacks->onRead(this); // Invoke the read callback.
404-
}
409+
// if (m_pCallbacks != nullptr) { // If is.long is false then this is the first (or only) request to read data, so invoke the callback
410+
// m_pCallbacks->onRead(this); // Invoke the read callback.
411+
// }
405412
}
406413
rsp.attr_value.handle = param->read.handle;
407414
rsp.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
@@ -530,6 +537,7 @@ void BLECharacteristic::notify(bool is_notification) {
530537
if(!is_notification)
531538
m_semaphoreConfEvt.wait("indicate");
532539
}
540+
delete(p2902);
533541
ESP_LOGD(LOG_TAG, "<< notify");
534542
} // Notify
535543

cpp_utils/BLECharacteristic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class BLECharacteristic {
105105
BLEService* m_pService;
106106
BLEValue m_value;
107107
esp_gatt_perm_t m_permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
108+
bool m_writeEvt = false;
108109

109110
void handleGATTServerEvent(
110111
esp_gatts_cb_event_t event,

cpp_utils/BLEClient.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ BLEClient::~BLEClient() {
8181
clearServices();
8282
esp_ble_gattc_app_unregister(m_gattc_if);
8383
BLEDevice::removePeerDevice(m_appId, true);
84+
if(m_deleteCallbacks)
85+
delete m_pClientCallbacks;
8486

8587
} // ~BLEClient
8688

@@ -171,15 +173,14 @@ void BLEClient::gattClientEventHandler(
171173
switch(event) {
172174

173175
case ESP_GATTC_SRVC_CHG_EVT:
174-
if(getConnId() != evtParam->search_res.conn_id)
176+
if(m_gattc_if != gattc_if)
175177
break;
176178

177179
ESP_LOGI(LOG_TAG, "SERVICE CHANGED");
178180
break;
179181

180-
case ESP_GATTC_CLOSE_EVT: {
182+
case ESP_GATTC_CLOSE_EVT:
181183
break;
182-
}
183184

184185
//
185186
// ESP_GATTC_DISCONNECT_EVT
@@ -189,8 +190,8 @@ void BLEClient::gattClientEventHandler(
189190
// - uint16_t conn_id
190191
// - esp_bd_addr_t remote_bda
191192
case ESP_GATTC_DISCONNECT_EVT: {
192-
ESP_LOGE(__func__, "disconnect event, conn_id: %d", evtParam->disconnect.conn_id);
193-
if(getConnId() != evtParam->disconnect.conn_id)
193+
ESP_LOGE(__func__, "disconnect event, reason: %d, connId: %d, my connId: %d, my IF: %d, gattc_if: %d", (int)evtParam->disconnect.reason, evtParam->disconnect.conn_id, getConnId(), getGattcIf(), gattc_if);
194+
if(m_gattc_if != gattc_if)
194195
break;
195196
m_semaphoreOpenEvt.give(evtParam->disconnect.reason);
196197
if(!m_isConnected)
@@ -214,7 +215,7 @@ void BLEClient::gattClientEventHandler(
214215
// - esp_bd_addr_t remote_bda
215216
//
216217
case ESP_GATTC_OPEN_EVT: {
217-
if(getConnId() != ESP_GATT_IF_NONE)
218+
if(m_gattc_if != gattc_if)
218219
break;
219220
m_conn_id = evtParam->open.conn_id;
220221
if (m_pClientCallbacks != nullptr) {
@@ -240,7 +241,6 @@ void BLEClient::gattClientEventHandler(
240241
if(m_appId == evtParam->reg.app_id){
241242
ESP_LOGI(__func__, "register app id: %d, %d, gattc_if: %d", m_appId, evtParam->reg.app_id, gattc_if);
242243
m_gattc_if = gattc_if;
243-
m_appId = evtParam->reg.app_id;
244244
m_semaphoreRegEvt.give();
245245
}
246246
break;
@@ -255,8 +255,9 @@ void BLEClient::gattClientEventHandler(
255255
break;
256256

257257
case ESP_GATTC_CONNECT_EVT: {
258-
if(evtParam->connect.conn_id != getConnId())
258+
if(m_gattc_if != gattc_if)
259259
break;
260+
m_conn_id = evtParam->connect.conn_id;
260261
BLEDevice::updatePeerDevice(this, true, m_gattc_if);
261262
esp_err_t errRc = esp_ble_gattc_send_mtu_req(gattc_if, evtParam->connect.conn_id);
262263
if (errRc != ESP_OK) {
@@ -278,7 +279,7 @@ void BLEClient::gattClientEventHandler(
278279
// - uint16_t conn_id
279280
//
280281
case ESP_GATTC_SEARCH_CMPL_EVT: {
281-
if(evtParam->search_cmpl.conn_id != getConnId())
282+
if(m_gattc_if != gattc_if)
282283
break;
283284
if (evtParam->search_cmpl.status != ESP_GATT_OK){
284285
ESP_LOGE(LOG_TAG, "search service failed, error status = %x", evtParam->search_cmpl.status);
@@ -308,8 +309,9 @@ void BLEClient::gattClientEventHandler(
308309
// - esp_gatt_id_t srvc_id
309310
//
310311
case ESP_GATTC_SEARCH_RES_EVT: {
311-
if(getConnId() != evtParam->search_res.conn_id)
312+
if(m_gattc_if != gattc_if)
312313
break;
314+
313315
BLEUUID uuid = BLEUUID(evtParam->search_res.srvc_id);
314316
BLERemoteService* pRemoteService = new BLERemoteService(
315317
evtParam->search_res.srvc_id,
@@ -515,8 +517,9 @@ bool BLEClient::isConnected() {
515517
/**
516518
* @brief Set the callbacks that will be invoked.
517519
*/
518-
void BLEClient::setClientCallbacks(BLEClientCallbacks* pClientCallbacks) {
520+
void BLEClient::setClientCallbacks(BLEClientCallbacks* pClientCallbacks, bool deleteCallbacks) {
519521
m_pClientCallbacks = pClientCallbacks;
522+
m_deleteCallbacks = deleteCallbacks;
520523
} // setClientCallbacks
521524

522525

cpp_utils/BLEClient.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class BLEClient {
5050

5151
bool isConnected(); // Return true if we are connected.
5252

53-
void setClientCallbacks(BLEClientCallbacks *pClientCallbacks);
53+
void setClientCallbacks(BLEClientCallbacks *pClientCallbacks, bool deleteCallbacks = true);
5454
void setValue(BLEUUID serviceUUID, BLEUUID characteristicUUID, std::string value); // Set the value of a given characteristic at a given service.
5555

5656
std::string toString(); // Return a string representation of this client.
@@ -72,8 +72,9 @@ uint16_t m_appId;
7272
esp_gatt_if_t m_gattc_if;
7373
bool m_haveServices = false; // Have we previously obtain the set of services from the remote server.
7474
bool m_isConnected = false; // Are we currently connected.
75+
bool m_deleteCallbacks = true;
7576

76-
BLEClientCallbacks* m_pClientCallbacks;
77+
BLEClientCallbacks* m_pClientCallbacks = nullptr;
7778
FreeRTOS::Semaphore m_semaphoreRegEvt = FreeRTOS::Semaphore("RegEvt");
7879
FreeRTOS::Semaphore m_semaphoreOpenEvt = FreeRTOS::Semaphore("OpenEvt");
7980
FreeRTOS::Semaphore m_semaphoreSearchCmplEvt = FreeRTOS::Semaphore("SearchCmplEvt");

cpp_utils/BLEDevice.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static const char* LOG_TAG = "BLEDevice";
4242
/**
4343
* Singletons for the BLEDevice.
4444
*/
45-
// BLEServer* BLEDevice::m_pServer = nullptr;
45+
BLEServer* BLEDevice::m_pServer = nullptr;
4646
BLEScan* BLEDevice::m_pScan = nullptr;
4747
// BLEClient* BLEDevice::m_pClient = nullptr;
4848
bool initialized = false;
@@ -82,7 +82,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
8282
ESP_LOGE(LOG_TAG, "BLE GATTS is not enabled - CONFIG_GATTS_ENABLE not defined");
8383
abort();
8484
#endif // CONFIG_GATTS_ENABLE
85-
BLEServer* m_pServer = new BLEServer();
85+
BLEDevice::m_pServer = new BLEServer();
8686
m_pServer->createApp(m_appId++);
8787
ESP_LOGD(LOG_TAG, "<< createServer");
8888
return m_pServer;
@@ -266,10 +266,6 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
266266
}
267267
} // switch
268268

269-
if (BLEDevice::m_pClient != nullptr) {
270-
BLEDevice::m_pClient->handleGAPEvent(event, param);
271-
}
272-
273269
if (BLEDevice::m_pScan != nullptr) {
274270
BLEDevice::getScan()->handleGAPEvent(event, param);
275271
}

cpp_utils/BLEDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class BLEDevice {
6565
static esp_ble_sec_act_t m_securityLevel;
6666

6767
private:
68-
// static BLEServer* m_pServer;
68+
static BLEServer* m_pServer;
6969
static BLEScan* m_pScan;
7070
// static BLEClient* m_pClient;
7171
static BLESecurityCallbacks* m_securityCallbacks;

cpp_utils/BLERemoteCharacteristic.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ BLERemoteCharacteristic::BLERemoteCharacteristic(
5757
*/
5858
BLERemoteCharacteristic::~BLERemoteCharacteristic() {
5959
removeDescriptors(); // Release resources for any descriptor information we may have allocated.
60+
if(m_rawData != nullptr) free(m_rawData);
6061
} // ~BLERemoteCharacteristic
6162

6263

cpp_utils/BLERemoteCharacteristic.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class BLERemoteCharacteristic {
5151
bool writeValue(uint8_t newValue, bool response = false);
5252
std::string toString();
5353
uint8_t* readRawData();
54+
BLERemoteService* getRemoteService();
5455

5556
private:
5657
BLERemoteCharacteristic(uint16_t handle, BLEUUID uuid, esp_gatt_char_prop_t charProp, BLERemoteService* pRemoteService);
@@ -61,7 +62,6 @@ class BLERemoteCharacteristic {
6162
// Private member functions
6263
void gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam);
6364

64-
BLERemoteService* getRemoteService();
6565
void removeDescriptors();
6666
void retrieveDescriptors();
6767

@@ -74,8 +74,8 @@ class BLERemoteCharacteristic {
7474
FreeRTOS::Semaphore m_semaphoreRegForNotifyEvt = FreeRTOS::Semaphore("RegForNotifyEvt");
7575
FreeRTOS::Semaphore m_semaphoreWriteCharEvt = FreeRTOS::Semaphore("WriteCharEvt");
7676
std::string m_value;
77-
uint8_t *m_rawData;
78-
notify_callback m_notifyCallback;
77+
uint8_t *m_rawData = nullptr;
78+
notify_callback m_notifyCallback = nullptr;
7979

8080
// We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID.
8181
std::map<std::string, BLERemoteDescriptor*> m_descriptorMap;

cpp_utils/BLEServer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#if defined(CONFIG_BT_ENABLED)
1010
#include <esp_bt.h>
1111
#include <esp_bt_main.h>
12+
#include <esp_gatts_api.h>
1213
#include "GeneralUtils.h"
1314
#include "BLEDevice.h"
1415
#include "BLEServer.h"
@@ -421,4 +422,9 @@ void BLEServer::updateConnParams(esp_bd_addr_t remote_bda, uint16_t minInterval,
421422
conn_params.timeout = timeout; // timeout = 400*10ms = 4000ms
422423
esp_ble_gap_update_conn_params(&conn_params);
423424
}
425+
426+
void BLEServer::disconnect(uint16_t connId){
427+
esp_ble_gatts_close(m_gatts_if, connId);
428+
}
429+
424430
#endif // CONFIG_BT_ENABLED

cpp_utils/BLEServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class BLEServer {
7272
BLEService* getServiceByUUID(const char* uuid);
7373
BLEService* getServiceByUUID(BLEUUID uuid);
7474
bool connect(BLEAddress address);
75+
void disconnect(uint16_t connId);
7576
uint16_t m_appId;
7677
void updateConnParams(esp_bd_addr_t remote_bda, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout);
7778

0 commit comments

Comments
 (0)