Skip to content

Commit ac94b34

Browse files
authored
Merge pull request #789 from chegewara/master
Bugfixes
2 parents 69fb1c6 + e6d9ff2 commit ac94b34

21 files changed

+172
-172
lines changed

cpp_utils/BLEAdvertisedDevice.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
*/
1414
#include "sdkconfig.h"
1515
#if defined(CONFIG_BT_ENABLED)
16-
#include <esp_log.h>
1716
#include <sstream>
1817
#include "BLEAdvertisedDevice.h"
1918
#include "BLEUtils.h"
20-
#ifdef ARDUINO_ARCH_ESP32
19+
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
2120
#include "esp32-hal-log.h"
22-
#endif
21+
#define LOG_TAG ""
22+
#else
23+
#include "esp_log.h"
2324
static const char* LOG_TAG="BLEAdvertisedDevice";
25+
#endif
2426

2527
BLEAdvertisedDevice::BLEAdvertisedDevice() {
2628
m_adFlag = 0;
@@ -523,5 +525,8 @@ size_t BLEAdvertisedDevice::getPayloadLength() {
523525
return m_payloadLength;
524526
}
525527

528+
void BLEAdvertisedDeviceCallbacks::onResult(BLEAdvertisedDevice dev) {}
529+
void BLEAdvertisedDeviceCallbacks::onResult(BLEAdvertisedDevice* dev) {}
530+
526531
#endif /* CONFIG_BT_ENABLED */
527532

cpp_utils/BLEAdvertisedDevice.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ class BLEAdvertisedDeviceCallbacks {
116116
* As we are scanning, we will find new devices. When found, this call back is invoked with a reference to the
117117
* device that was found. During any individual scan, a device will only be detected one time.
118118
*/
119-
virtual void onResult(BLEAdvertisedDevice advertisedDevice) = 0;
119+
virtual void onResult(BLEAdvertisedDevice advertisedDevice);
120+
virtual void onResult(BLEAdvertisedDevice* advertisedDevice);
120121
};
121122

122123
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLEAdvertising.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
#include "sdkconfig.h"
2020
#if defined(CONFIG_BT_ENABLED)
2121
#include "BLEAdvertising.h"
22-
#include <esp_log.h>
2322
#include <esp_err.h>
2423
#include "BLEUtils.h"
2524
#include "GeneralUtils.h"
2625

27-
#ifdef ARDUINO_ARCH_ESP32
26+
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
2827
#include "esp32-hal-log.h"
28+
#define LOG_TAG ""
29+
#else
30+
#include "esp_log.h"
31+
static const char* LOG_TAG = "BLEAdvertising";
2932
#endif
3033

31-
static const char* LOG_TAG = "BLEAdvertising";
3234

3335

3436
/**

cpp_utils/BLEBeacon.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
#include "sdkconfig.h"
88
#if defined(CONFIG_BT_ENABLED)
99
#include <string.h>
10-
#include <esp_log.h>
1110
#include "BLEBeacon.h"
11+
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
12+
#include "esp32-hal-log.h"
13+
#define LOG_TAG ""
14+
#else
15+
#include "esp_log.h"
16+
static const char* LOG_TAG = "BLEBeacon";
17+
#endif
1218

1319
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
1420

15-
static const char LOG_TAG[] = "BLEBeacon";
1621

1722
BLEBeacon::BLEBeacon() {
1823
m_beaconData.manufacturerId = 0x4c00;

cpp_utils/BLECharacteristic.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@
1111
#include <iomanip>
1212
#include <stdlib.h>
1313
#include "sdkconfig.h"
14-
#include <esp_log.h>
1514
#include <esp_err.h>
1615
#include "BLECharacteristic.h"
1716
#include "BLEService.h"
1817
#include "BLEDevice.h"
1918
#include "BLEUtils.h"
2019
#include "BLE2902.h"
2120
#include "GeneralUtils.h"
22-
#ifdef ARDUINO_ARCH_ESP32
21+
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
2322
#include "esp32-hal-log.h"
24-
#endif
25-
23+
#define LOG_TAG ""
24+
#else
25+
#include "esp_log.h"
2626
static const char* LOG_TAG = "BLECharacteristic";
27+
#endif
2728

2829
#define NULL_HANDLE (0xffff)
2930

cpp_utils/BLEClient.cpp

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
#include "sdkconfig.h"
88
#if defined(CONFIG_BT_ENABLED)
9-
#include <esp_log.h>
109
#include <esp_bt.h>
1110
#include <esp_bt_main.h>
1211
#include <esp_gap_ble_api.h>
@@ -19,10 +18,15 @@
1918
#include <sstream>
2019
#include <unordered_set>
2120
#include "BLEDevice.h"
22-
#ifdef ARDUINO_ARCH_ESP32
21+
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
2322
#include "esp32-hal-log.h"
23+
#define LOG_TAG ""
24+
#else
25+
#include "esp_log.h"
26+
static const char* LOG_TAG = "BLEClient";
2427
#endif
2528

29+
2630
/*
2731
* Design
2832
* ------
@@ -43,14 +47,28 @@
4347
*
4448
*
4549
*/
46-
static const char* LOG_TAG = "BLEClient";
4750

4851
BLEClient::BLEClient() {
4952
m_pClientCallbacks = nullptr;
5053
m_conn_id = ESP_GATT_IF_NONE;
5154
m_gattc_if = ESP_GATT_IF_NONE;
5255
m_haveServices = false;
5356
m_isConnected = false; // Initially, we are flagged as not connected.
57+
58+
59+
m_appId = BLEDevice::m_appId++;
60+
m_appId = m_appId%100;
61+
BLEDevice::addPeerDevice(this, true, m_appId);
62+
m_semaphoreRegEvt.take("connect");
63+
64+
esp_err_t errRc = ::esp_ble_gattc_app_register(m_appId);
65+
if (errRc != ESP_OK) {
66+
ESP_LOGE(LOG_TAG, "esp_ble_gattc_app_register: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
67+
return;
68+
}
69+
70+
m_semaphoreRegEvt.wait("connect");
71+
5472
} // BLEClient
5573

5674

@@ -60,10 +78,10 @@ BLEClient::BLEClient() {
6078
BLEClient::~BLEClient() {
6179
// We may have allocated service references associated with this client. Before we are finished
6280
// with the client, we must release resources.
63-
for (auto &myPair : m_servicesMap) {
64-
delete myPair.second;
65-
}
66-
m_servicesMap.clear();
81+
clearServices();
82+
esp_ble_gattc_app_unregister(m_gattc_if);
83+
BLEDevice::removePeerDevice(m_appId, true);
84+
6785
} // ~BLEClient
6886

6987

@@ -78,6 +96,7 @@ void BLEClient::clearServices() {
7896
delete myPair.second;
7997
}
8098
m_servicesMap.clear();
99+
m_servicesMapByInstID.clear();
81100
m_haveServices = false;
82101
ESP_LOGD(LOG_TAG, "<< clearServices");
83102
} // clearServices
@@ -99,26 +118,12 @@ bool BLEClient::connect(BLEAdvertisedDevice* device) {
99118
bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
100119
ESP_LOGD(LOG_TAG, ">> connect(%s)", address.toString().c_str());
101120

102-
// We need the connection handle that we get from registering the application. We register the app
103-
// and then block on its completion. When the event has arrived, we will have the handle.
104-
m_appId = BLEDevice::m_appId++;
105-
BLEDevice::addPeerDevice(this, true, m_appId);
106-
m_semaphoreRegEvt.take("connect");
107-
108-
// clearServices(); // we dont need to delete services since every client is unique?
109-
esp_err_t errRc = ::esp_ble_gattc_app_register(m_appId);
110-
if (errRc != ESP_OK) {
111-
ESP_LOGE(LOG_TAG, "esp_ble_gattc_app_register: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
112-
return false;
113-
}
114-
115-
m_semaphoreRegEvt.wait("connect");
116-
121+
clearServices();
117122
m_peerAddress = address;
118123

119124
// Perform the open connection request against the target BLE Server.
120125
m_semaphoreOpenEvt.take("connect");
121-
errRc = ::esp_ble_gattc_open(
126+
esp_err_t errRc = ::esp_ble_gattc_open(
122127
m_gattc_if,
123128
*getPeerAddress().getNative(), // address
124129
type, // Note: This was added on 2018-04-03 when the latest ESP-IDF was detected to have changed the signature.
@@ -141,6 +146,7 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
141146
*/
142147
void BLEClient::disconnect() {
143148
ESP_LOGD(LOG_TAG, ">> disconnect()");
149+
// ESP_LOGW(__func__, "gattIf: %d, connId: %d", getGattcIf(), getConnId());
144150
esp_err_t errRc = ::esp_ble_gattc_close(getGattcIf(), getConnId());
145151
if (errRc != ESP_OK) {
146152
ESP_LOGE(LOG_TAG, "esp_ble_gattc_close: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -165,12 +171,13 @@ void BLEClient::gattClientEventHandler(
165171
switch(event) {
166172

167173
case ESP_GATTC_SRVC_CHG_EVT:
174+
if(getConnId() != evtParam->search_res.conn_id)
175+
break;
176+
168177
ESP_LOGI(LOG_TAG, "SERVICE CHANGED");
169178
break;
170179

171180
case ESP_GATTC_CLOSE_EVT: {
172-
// esp_ble_gattc_app_unregister(m_appId);
173-
// BLEDevice::removePeerDevice(m_gattc_if, true);
174181
break;
175182
}
176183

@@ -197,7 +204,7 @@ void BLEClient::gattClientEventHandler(
197204
}
198205
break;
199206
} // ESP_GATTC_DISCONNECT_EVT
200-
207+
201208
//
202209
// ESP_GATTC_OPEN_EVT
203210
//
@@ -207,12 +214,15 @@ void BLEClient::gattClientEventHandler(
207214
// - esp_bd_addr_t remote_bda
208215
//
209216
case ESP_GATTC_OPEN_EVT: {
217+
if(getConnId() != ESP_GATT_IF_NONE)
218+
break;
210219
m_conn_id = evtParam->open.conn_id;
211220
if (m_pClientCallbacks != nullptr) {
212221
m_pClientCallbacks->onConnect(this);
213222
}
214223
if (evtParam->open.status == ESP_GATT_OK) {
215224
m_isConnected = true; // Flag us as connected.
225+
m_mtu = evtParam->open.mtu;
216226
}
217227
m_semaphoreOpenEvt.give(evtParam->open.status);
218228
break;
@@ -240,10 +250,13 @@ void BLEClient::gattClientEventHandler(
240250
if(evtParam->cfg_mtu.status != ESP_GATT_OK) {
241251
ESP_LOGE(LOG_TAG,"Config mtu failed");
242252
}
243-
m_mtu = evtParam->cfg_mtu.mtu;
253+
else
254+
m_mtu = evtParam->cfg_mtu.mtu;
244255
break;
245256

246257
case ESP_GATTC_CONNECT_EVT: {
258+
if(evtParam->connect.conn_id != getConnId())
259+
break;
247260
BLEDevice::updatePeerDevice(this, true, m_gattc_if);
248261
esp_err_t errRc = esp_ble_gattc_send_mtu_req(gattc_if, evtParam->connect.conn_id);
249262
if (errRc != ESP_OK) {
@@ -265,10 +278,10 @@ void BLEClient::gattClientEventHandler(
265278
// - uint16_t conn_id
266279
//
267280
case ESP_GATTC_SEARCH_CMPL_EVT: {
268-
esp_ble_gattc_cb_param_t* p_data = (esp_ble_gattc_cb_param_t*)evtParam;
269-
if (p_data->search_cmpl.status != ESP_GATT_OK){
270-
ESP_LOGE(LOG_TAG, "search service failed, error status = %x", p_data->search_cmpl.status);
281+
if(evtParam->search_cmpl.conn_id != getConnId())
271282
break;
283+
if (evtParam->search_cmpl.status != ESP_GATT_OK){
284+
ESP_LOGE(LOG_TAG, "search service failed, error status = %x", evtParam->search_cmpl.status);
272285
}
273286
#ifndef ARDUINO_ARCH_ESP32
274287
// commented out just for now to keep backward compatibility
@@ -280,7 +293,7 @@ void BLEClient::gattClientEventHandler(
280293
// ESP_LOGI(LOG_TAG, "unknown service source");
281294
// }
282295
#endif
283-
m_semaphoreSearchCmplEvt.give(0);
296+
m_semaphoreSearchCmplEvt.give(evtParam->search_cmpl.status);
284297
break;
285298
} // ESP_GATTC_SEARCH_CMPL_EVT
286299

@@ -295,6 +308,8 @@ void BLEClient::gattClientEventHandler(
295308
// - esp_gatt_id_t srvc_id
296309
//
297310
case ESP_GATTC_SEARCH_RES_EVT: {
311+
if(getConnId() != evtParam->search_res.conn_id)
312+
break;
298313
BLEUUID uuid = BLEUUID(evtParam->search_res.srvc_id);
299314
BLERemoteService* pRemoteService = new BLERemoteService(
300315
evtParam->search_res.srvc_id,

cpp_utils/BLEClient.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,10 @@ uint16_t m_appId;
6565
friend class BLERemoteCharacteristic;
6666
friend class BLERemoteDescriptor;
6767

68-
void gattClientEventHandler(
69-
esp_gattc_cb_event_t event,
70-
esp_gatt_if_t gattc_if,
71-
esp_ble_gattc_cb_param_t* param);
68+
void gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* param);
7269

7370
BLEAddress m_peerAddress = BLEAddress((uint8_t*)"\0\0\0\0\0\0"); // The BD address of the remote server.
7471
uint16_t m_conn_id;
75-
// int m_deviceType;
7672
esp_gatt_if_t m_gattc_if;
7773
bool m_haveServices = false; // Have we previously obtain the set of services from the remote server.
7874
bool m_isConnected = false; // Are we currently connected.

cpp_utils/BLEDescriptor.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@
1111
#include <iomanip>
1212
#include <stdlib.h>
1313
#include "sdkconfig.h"
14-
#include <esp_log.h>
1514
#include <esp_err.h>
1615
#include "BLEService.h"
1716
#include "BLEDescriptor.h"
1817
#include "GeneralUtils.h"
19-
#ifdef ARDUINO_ARCH_ESP32
18+
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
2019
#include "esp32-hal-log.h"
20+
#define LOG_TAG ""
21+
#else
22+
#include "esp_log.h"
23+
static const char* LOG_TAG = "BLEDescriptor";
2124
#endif
2225

23-
static const char* LOG_TAG = "BLEDescriptor";
26+
2427

2528

2629
#define NULL_HANDLE (0xffff)

0 commit comments

Comments
 (0)