12
12
#include < bt.h> // ESP32 BLE
13
13
#include < esp_bt_main.h> // ESP32 BLE
14
14
#include < esp_gap_ble_api.h> // ESP32 BLE
15
- # include < esp_gattc_api.h > // ESP32 BLE
15
+ // ESP32 BLE
16
16
#include < esp_gatts_api.h> // ESP32 BLE
17
17
#include < esp_err.h> // ESP32 ESP-IDF
18
18
#include < esp_log.h> // ESP32 ESP-IDF
23
23
#include " BLE.h"
24
24
#include " BLEClient.h"
25
25
#include " BLEUtils.h"
26
- #include " BLEXXXCharacteristic.h"
27
26
#include " GeneralUtils.h"
28
27
29
28
static char LOG_TAG[] = " BLE" ;
30
29
31
-
32
- static EventGroupHandle_t g_eventGroup;
33
- #define EVENT_GROUP_SCAN_COMPLETE (1 <<0 )
34
-
35
-
36
- // static esp_gatt_if_t g_gattc_if;
37
-
38
- /*
39
- * We maintain a map of found devices. The map is keyed off the 6 byte address value of the device.
40
- * Note that this address is binary and should not be directly printed. The value of the map is
41
- * the BLEDevice object that this device represents.
42
- */
43
- static std::map<std::string, BLEClient> g_devices;
44
-
45
30
BLEServer *BLE::m_bleServer = nullptr ;
46
31
BLEScan *BLE::m_pScan = nullptr ;
47
32
BLEClient *BLE::m_pClient = nullptr ;
48
33
34
+ #include < esp_gattc_api.h>
35
+
49
36
BLE::BLE () {
50
37
}
51
38
@@ -57,21 +44,7 @@ BLE::~BLE() {
57
44
BLEClient* BLE::createClient () {
58
45
m_pClient = new BLEClient ();
59
46
return m_pClient;
60
- }
61
-
62
-
63
- /* *
64
- * @brief Dump all the devices.
65
- *
66
- * During scanning, a map is built containing all the unique devices found. Calling this function
67
- * will dump the details of all the devices.
68
- */
69
- void BLE::dumpDevices () {
70
- ESP_LOGD (LOG_TAG, " Number of devices: %d" , g_devices.size ());
71
- for (auto &myPair : g_devices ) {
72
- myPair.second .dump ();
73
- }
74
- } // dumpDevices
47
+ } // createClient
75
48
76
49
77
50
/* *
@@ -81,19 +54,19 @@ void BLE::dumpDevices() {
81
54
* @param [in] gatts_if
82
55
* @param [in] param
83
56
*/
84
- static void gatt_server_event_handler (
57
+ void BLE::gattServerEventHandler (
85
58
esp_gatts_cb_event_t event,
86
59
esp_gatt_if_t gatts_if,
87
60
esp_ble_gatts_cb_param_t *param
88
61
) {
89
- ESP_LOGD (LOG_TAG, " gatt_server_event_handler [esp_gatt_if: %d] ... %s" ,
62
+ ESP_LOGD (LOG_TAG, " gattServerEventHandler [esp_gatt_if: %d] ... %s" ,
90
63
gatts_if,
91
64
BLEUtils::gattServerEventTypeToString (event).c_str ());
92
65
BLEUtils::dumpGattServerEvent (event, gatts_if, param);
93
66
if (BLE::m_bleServer != nullptr ) {
94
67
BLE::m_bleServer->handleGATTServerEvent (event, gatts_if, param);
95
68
}
96
- } // gatt_server_event_handler
69
+ } // gattServerEventHandler
97
70
98
71
99
72
/* *
@@ -109,93 +82,16 @@ static void gatt_server_event_handler(
109
82
* @param [in] gattc_if
110
83
* @param [in] param
111
84
*/
112
- static void gatt_client_event_handler (
85
+ void BLE::gattClientEventHandler (
113
86
esp_gattc_cb_event_t event,
114
87
esp_gatt_if_t gattc_if,
115
88
esp_ble_gattc_cb_param_t *param) {
116
89
117
- ESP_LOGD (LOG_TAG, " gatt_client_event_handler [esp_gatt_if: %d] ... %s" ,
90
+ ESP_LOGD (LOG_TAG, " gattClientEventHandler [esp_gatt_if: %d] ... %s" ,
118
91
gattc_if, BLEUtils::gattClientEventTypeToString (event).c_str ());
119
92
BLEUtils::dumpGattClientEvent (event, gattc_if, param);
120
93
121
94
switch (event) {
122
- /*
123
- case ESP_GATTC_OPEN_EVT: {
124
- BLEClient *pDevice = BLEUtils::findByAddress(std::string((char *)param->open.remote_bda, 6));
125
- BLEUtils::registerByConnId(param->open.conn_id, pDevice);
126
- pDevice->dump();
127
- pDevice->onConnected(param->open.status);
128
- break;
129
- }
130
- */
131
-
132
- /*
133
- * The search_res field of the parameter has been populated. It contains:
134
- * * uint16_t conn_id - Can this be used to find the device?
135
- * * esp_gatt_srvc_id_t srvc_id
136
- * * esp_gatt_id_t id
137
- * * esp_bt_uuid_t uuid
138
- * * uint8_t inst_id
139
- * * bool is_primary
140
- */
141
-
142
- /*
143
- case ESP_GATTC_SEARCH_RES_EVT: {
144
- BLEClient *pDevice = BLEUtils::findByConnId(param->search_res.conn_id);
145
- pDevice->addService(param->search_res.srvc_id);
146
- break;
147
- }
148
-
149
- case ESP_GATTC_SEARCH_CMPL_EVT: {
150
- BLEClient *pDevice = BLEUtils::findByConnId(param->search_cmpl.conn_id);
151
- pDevice->onSearchComplete();
152
- break;
153
- }
154
- */
155
-
156
- /*
157
- * The `get_char` field of the parameters has been populated. It contains:
158
- * * esp_gatt_status_t status
159
- * * uint16_t conn_id
160
- * * esp_gatt_srvc_id_t srvc_id
161
- * * esp_gatt_id_t char_id
162
- * * esp_gatt_char_prop_t char_prop
163
- */
164
- /*
165
- case ESP_GATTC_GET_CHAR_EVT: {
166
- if (param->get_char.status == ESP_GATT_OK) {
167
- BLEClient *pDevice = BLEUtils::findByConnId(param->get_char.conn_id);
168
- BLECharacteristicXXX characteristic(param->get_char.conn_id,
169
- param->get_char.srvc_id, param->get_char.char_id, param->get_char.char_prop);
170
- pDevice->onCharacteristic(characteristic);
171
- }
172
- break;
173
- }
174
- */
175
-
176
-
177
- /*
178
- * The `read` field of the parameters has been populated. It contains:
179
- * * esp_gatt_status_t status
180
- * * uint16_t conn_id
181
- * * esp_gatt_srvc_id_t srvc_id
182
- * * esp_gatt_id_t char_id
183
- * * esp_gatt_id_t descr_id
184
- * * uint8_t *value
185
- * * uint16_t value_type
186
- * * uint16_t value_len
187
- */
188
- /*
189
- case ESP_GATTC_READ_CHAR_EVT: {
190
- if (param->read.status == ESP_GATT_OK) {
191
- BLEClient *pDevice = BLEUtils::findByConnId(param->read.conn_id);
192
- std::string data = std::string((char *)param->read.value, param->read.value_len);
193
- pDevice->onRead(data);
194
- }
195
- break;
196
- }
197
- */
198
-
199
95
default : {
200
96
break ;
201
97
}
@@ -206,13 +102,13 @@ static void gatt_client_event_handler(
206
102
BLE::m_pClient->gattClientEventHandler (event, gattc_if, param);
207
103
}
208
104
209
- } // gatt_event_handler
105
+ } // gattClientEventHandler
210
106
211
107
212
108
/* *
213
109
* @brief Handle GAP events.
214
110
*/
215
- static void gap_event_handler (
111
+ void BLE::gapEventHandler (
216
112
esp_gap_ble_cb_event_t event,
217
113
esp_ble_gap_cb_param_t *param) {
218
114
@@ -236,18 +132,10 @@ static void gap_event_handler(
236
132
BLE::m_bleServer->handleGAPEvent (event, param);
237
133
}
238
134
239
- if (BLE::getScan () != nullptr ) {
135
+ if (BLE::m_pScan != nullptr ) {
240
136
BLE::getScan ()->gapEventHandler (event, param);
241
137
}
242
- } // gap_event_handler
243
-
244
-
245
- /* *
246
- * @brief Get the current set of known devices.
247
- */
248
- std::map<std::string, BLEClient> getDevices () {
249
- return g_devices;
250
- } // getDevices
138
+ } // gapEventHandler
251
139
252
140
253
141
/* *
@@ -281,13 +169,13 @@ std::map<std::string, BLEClient> getDevices() {
281
169
return ;
282
170
}
283
171
284
- errRc = esp_ble_gap_register_callback (gap_event_handler );
172
+ errRc = esp_ble_gap_register_callback (BLE::gapEventHandler );
285
173
if (errRc != ESP_OK) {
286
174
ESP_LOGE (LOG_TAG, " esp_ble_gap_register_callback: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
287
175
return ;
288
176
}
289
177
290
- errRc = esp_ble_gatts_register_callback (gatt_server_event_handler );
178
+ errRc = esp_ble_gatts_register_callback (BLE::gattServerEventHandler );
291
179
if (errRc != ESP_OK) {
292
180
ESP_LOGE (LOG_TAG, " esp_ble_gatts_register_callback: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
293
181
return ;
@@ -307,7 +195,7 @@ std::map<std::string, BLEClient> getDevices() {
307
195
};
308
196
309
197
return ;
310
- }
198
+ } // initServer
311
199
312
200
313
201
/* *
@@ -321,7 +209,6 @@ void BLE::initClient() {
321
209
return ;
322
210
}
323
211
324
-
325
212
errRc = esp_bt_controller_enable (ESP_BT_MODE_BTDM);
326
213
if (errRc != ESP_OK) {
327
214
ESP_LOGE (LOG_TAG, " esp_bt_controller_enable: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
@@ -340,22 +227,25 @@ void BLE::initClient() {
340
227
return ;
341
228
}
342
229
343
- errRc = esp_ble_gap_register_callback (gap_event_handler );
230
+ errRc = esp_ble_gap_register_callback (BLE::gapEventHandler );
344
231
if (errRc != ESP_OK) {
345
232
ESP_LOGE (LOG_TAG, " esp_ble_gap_register_callback: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
346
233
return ;
347
234
}
348
235
349
- errRc = esp_ble_gattc_register_callback (gatt_client_event_handler );
236
+ errRc = esp_ble_gattc_register_callback (BLE::gattClientEventHandler );
350
237
if (errRc != ESP_OK) {
351
238
ESP_LOGE (LOG_TAG, " esp_ble_gattc_register_callback: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
352
239
return ;
353
240
}
354
241
355
- g_eventGroup = xEventGroupCreate ();
356
- xEventGroupClearBits (g_eventGroup, 0xff );
357
- } // init
242
+ } // initClient
243
+
358
244
245
+ /* *
246
+ * @brief Retrieve the Scan object that we use for scanning.
247
+ * @return The scanning object reference.
248
+ */
359
249
BLEScan* BLE::getScan () {
360
250
if (m_pScan == nullptr ) {
361
251
m_pScan = new BLEScan ();
0 commit comments