6
6
*/
7
7
#include " sdkconfig.h"
8
8
#if defined(CONFIG_BT_ENABLED)
9
- #include < esp_log.h>
10
9
#include < esp_bt.h>
11
10
#include < esp_bt_main.h>
12
11
#include < esp_gap_ble_api.h>
19
18
#include < sstream>
20
19
#include < unordered_set>
21
20
#include " BLEDevice.h"
22
- #ifdef ARDUINO_ARCH_ESP32
21
+ #if defined( ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
23
22
#include " esp32-hal-log.h"
23
+ #define LOG_TAG " "
24
+ #else
25
+ #include " esp_log.h"
26
+ static const char * LOG_TAG = " BLEClient" ;
24
27
#endif
25
28
29
+
26
30
/*
27
31
* Design
28
32
* ------
43
47
*
44
48
*
45
49
*/
46
- static const char * LOG_TAG = " BLEClient" ;
47
50
48
51
BLEClient::BLEClient () {
49
52
m_pClientCallbacks = nullptr ;
50
53
m_conn_id = ESP_GATT_IF_NONE;
51
54
m_gattc_if = ESP_GATT_IF_NONE;
52
55
m_haveServices = false ;
53
56
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
+
54
72
} // BLEClient
55
73
56
74
@@ -60,10 +78,10 @@ BLEClient::BLEClient() {
60
78
BLEClient::~BLEClient () {
61
79
// We may have allocated service references associated with this client. Before we are finished
62
80
// 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
+
67
85
} // ~BLEClient
68
86
69
87
@@ -78,6 +96,7 @@ void BLEClient::clearServices() {
78
96
delete myPair.second ;
79
97
}
80
98
m_servicesMap.clear ();
99
+ m_servicesMapByInstID.clear ();
81
100
m_haveServices = false ;
82
101
ESP_LOGD (LOG_TAG, " << clearServices" );
83
102
} // clearServices
@@ -99,26 +118,12 @@ bool BLEClient::connect(BLEAdvertisedDevice* device) {
99
118
bool BLEClient::connect (BLEAddress address, esp_ble_addr_type_t type) {
100
119
ESP_LOGD (LOG_TAG, " >> connect(%s)" , address.toString ().c_str ());
101
120
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 ();
117
122
m_peerAddress = address;
118
123
119
124
// Perform the open connection request against the target BLE Server.
120
125
m_semaphoreOpenEvt.take (" connect" );
121
- errRc = ::esp_ble_gattc_open (
126
+ esp_err_t errRc = ::esp_ble_gattc_open (
122
127
m_gattc_if,
123
128
*getPeerAddress ().getNative (), // address
124
129
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) {
141
146
*/
142
147
void BLEClient::disconnect () {
143
148
ESP_LOGD (LOG_TAG, " >> disconnect()" );
149
+ // ESP_LOGW(__func__, "gattIf: %d, connId: %d", getGattcIf(), getConnId());
144
150
esp_err_t errRc = ::esp_ble_gattc_close (getGattcIf (), getConnId ());
145
151
if (errRc != ESP_OK) {
146
152
ESP_LOGE (LOG_TAG, " esp_ble_gattc_close: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
@@ -165,12 +171,13 @@ void BLEClient::gattClientEventHandler(
165
171
switch (event) {
166
172
167
173
case ESP_GATTC_SRVC_CHG_EVT:
174
+ if (getConnId () != evtParam->search_res .conn_id )
175
+ break ;
176
+
168
177
ESP_LOGI (LOG_TAG, " SERVICE CHANGED" );
169
178
break ;
170
179
171
180
case ESP_GATTC_CLOSE_EVT: {
172
- // esp_ble_gattc_app_unregister(m_appId);
173
- // BLEDevice::removePeerDevice(m_gattc_if, true);
174
181
break ;
175
182
}
176
183
@@ -197,7 +204,7 @@ void BLEClient::gattClientEventHandler(
197
204
}
198
205
break ;
199
206
} // ESP_GATTC_DISCONNECT_EVT
200
-
207
+
201
208
//
202
209
// ESP_GATTC_OPEN_EVT
203
210
//
@@ -207,12 +214,15 @@ void BLEClient::gattClientEventHandler(
207
214
// - esp_bd_addr_t remote_bda
208
215
//
209
216
case ESP_GATTC_OPEN_EVT: {
217
+ if (getConnId () != ESP_GATT_IF_NONE)
218
+ break ;
210
219
m_conn_id = evtParam->open .conn_id ;
211
220
if (m_pClientCallbacks != nullptr ) {
212
221
m_pClientCallbacks->onConnect (this );
213
222
}
214
223
if (evtParam->open .status == ESP_GATT_OK) {
215
224
m_isConnected = true ; // Flag us as connected.
225
+ m_mtu = evtParam->open .mtu ;
216
226
}
217
227
m_semaphoreOpenEvt.give (evtParam->open .status );
218
228
break ;
@@ -240,10 +250,13 @@ void BLEClient::gattClientEventHandler(
240
250
if (evtParam->cfg_mtu .status != ESP_GATT_OK) {
241
251
ESP_LOGE (LOG_TAG," Config mtu failed" );
242
252
}
243
- m_mtu = evtParam->cfg_mtu .mtu ;
253
+ else
254
+ m_mtu = evtParam->cfg_mtu .mtu ;
244
255
break ;
245
256
246
257
case ESP_GATTC_CONNECT_EVT: {
258
+ if (evtParam->connect .conn_id != getConnId ())
259
+ break ;
247
260
BLEDevice::updatePeerDevice (this , true , m_gattc_if);
248
261
esp_err_t errRc = esp_ble_gattc_send_mtu_req (gattc_if, evtParam->connect .conn_id );
249
262
if (errRc != ESP_OK) {
@@ -265,10 +278,10 @@ void BLEClient::gattClientEventHandler(
265
278
// - uint16_t conn_id
266
279
//
267
280
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 ())
271
282
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 );
272
285
}
273
286
#ifndef ARDUINO_ARCH_ESP32
274
287
// commented out just for now to keep backward compatibility
@@ -280,7 +293,7 @@ void BLEClient::gattClientEventHandler(
280
293
// ESP_LOGI(LOG_TAG, "unknown service source");
281
294
// }
282
295
#endif
283
- m_semaphoreSearchCmplEvt.give (0 );
296
+ m_semaphoreSearchCmplEvt.give (evtParam-> search_cmpl . status );
284
297
break ;
285
298
} // ESP_GATTC_SEARCH_CMPL_EVT
286
299
@@ -295,6 +308,8 @@ void BLEClient::gattClientEventHandler(
295
308
// - esp_gatt_id_t srvc_id
296
309
//
297
310
case ESP_GATTC_SEARCH_RES_EVT: {
311
+ if (getConnId () != evtParam->search_res .conn_id )
312
+ break ;
298
313
BLEUUID uuid = BLEUUID (evtParam->search_res .srvc_id );
299
314
BLERemoteService* pRemoteService = new BLERemoteService (
300
315
evtParam->search_res .srvc_id ,
0 commit comments