13
13
#include < esp_bt_main.h> // ESP32 BLE
14
14
#include < esp_gap_ble_api.h> // ESP32 BLE
15
15
#include < esp_gattc_api.h> // ESP32 BLE
16
+ #include < esp_gatts_api.h> // ESP32 BLE
16
17
#include < esp_err.h> // ESP32 ESP-IDF
17
18
#include < esp_log.h> // ESP32 ESP-IDF
18
19
#include < map> // Part of C++ STL
@@ -43,6 +44,8 @@ static EventGroupHandle_t g_eventGroup;
43
44
*/
44
45
static std::map<ble_address, BLEDevice> g_devices;
45
46
47
+ BLEServer *BLE::m_server;
48
+
46
49
BLE::BLE () {
47
50
}
48
51
@@ -166,24 +169,48 @@ static void dump_adv_payload(uint8_t *payload) {
166
169
*/
167
170
168
171
172
+ /* *
173
+ * @brief Handle GATT server events.
174
+ *
175
+ * @param [in] event
176
+ * @param [in] gatts_if
177
+ * @param [in] param
178
+ */
179
+ void gatt_server_event_handler (
180
+ esp_gatts_cb_event_t event,
181
+ esp_gatt_if_t gatts_if,
182
+ esp_ble_gatts_cb_param_t *param
183
+ ) {
184
+ ESP_LOGD (tag, " gatt_server_event_handler [esp_gatt_if: %d] ... %s" ,
185
+ gatts_if, bt_utils_gatt_server_event_type_to_string (event).c_str ());
186
+ BLEUtils::dumpGattServerEvent (event, gatts_if, param);
187
+ if (BLE::m_server != nullptr ) {
188
+ BLE::m_server->handleGATTServerEvent (event, gatts_if, param);
189
+ }
190
+ } // gatt_server_event_handler
191
+
169
192
170
193
/* *
171
- * @brief Handle GATT events.
194
+ * @brief Handle GATT client events.
172
195
*
173
- * Handler for the GATT events.
196
+ * Handler for the GATT client events.
174
197
* * `ESP_GATTC_OPEN_EVT` – Invoked when a connection is opened.
175
198
* * `ESP_GATTC_PREP_WRITE_EVT` – Response to write a characteristic.
176
199
* * `ESP_GATTC_READ_CHAR_EVT` – Response to read a characteristic.
177
200
* * `ESP_GATTC_REG_EVT` – Invoked when a GATT client has been registered.
201
+ *
202
+ * @param [in] event
203
+ * @param [in] gattc_if
204
+ * @param [in] param
178
205
*/
179
- static void gatt_event_handler (
206
+ static void gatt_client_event_handler (
180
207
esp_gattc_cb_event_t event,
181
208
esp_gatt_if_t gattc_if,
182
209
esp_ble_gattc_cb_param_t *param) {
183
210
184
- ESP_LOGD (tag, " gatt_event_handler [esp_gatt_if: %d] ... %s" ,
185
- gattc_if, bt_utils_gatt_event_type_to_string (event).c_str ());
186
- BLEUtils::dumpGattEvent (event, gattc_if, param);
211
+ ESP_LOGD (tag, " gatt_client_event_handler [esp_gatt_if: %d] ... %s" ,
212
+ gattc_if, bt_utils_gatt_client_event_type_to_string (event).c_str ());
213
+ BLEUtils::dumpGattClientEvent (event, gattc_if, param);
187
214
188
215
switch (event) {
189
216
case ESP_GATTC_OPEN_EVT: {
@@ -257,6 +284,7 @@ static void gatt_event_handler(
257
284
default :
258
285
break ;
259
286
}
287
+
260
288
} // gatt_event_handler
261
289
262
290
@@ -277,6 +305,19 @@ static void gap_event_handler(
277
305
ESP_LOGD (tag, " status: %d" , param->scan_start_cmpl .status );
278
306
}
279
307
308
+ else if (event == ESP_GAP_BLE_AUTH_CMPL_EVT) {
309
+ // key is a 16 byte value
310
+
311
+ ESP_LOGD (tag, " [bd_addr: %s, key_present: %d, key: ***, key_type: %d, success: %d, fail_reason: %d, addr_type: ***, dev_type: %s]" ,
312
+ BLEUtils::addressToString (param->ble_security .auth_cmpl .bd_addr ).c_str (),
313
+ param->ble_security .auth_cmpl .key_present ,
314
+ param->ble_security .auth_cmpl .key_type ,
315
+ param->ble_security .auth_cmpl .success ,
316
+ param->ble_security .auth_cmpl .fail_reason ,
317
+ BLEUtils::devTypeToString (param->ble_security .auth_cmpl .dev_type ).c_str ()
318
+ );
319
+ }
320
+
280
321
else if (event == ESP_GAP_BLE_SCAN_RESULT_EVT) {
281
322
BLEDevice device;
282
323
@@ -304,6 +345,9 @@ static void gap_event_handler(
304
345
ESP_LOGD (tag, " Unhandled search_evt type!" );
305
346
}
306
347
} // ESP_GAP_BLE_SCAN_RESULT_EVT
348
+ if (BLE::m_server != nullptr ) {
349
+ BLE::m_server->handleGAPEvent (event, param);
350
+ }
307
351
} // gap_event_handler
308
352
309
353
@@ -314,14 +358,63 @@ std::map<ble_address, BLEDevice> getDevices() {
314
358
return g_devices;
315
359
} // getDevices
316
360
361
+ /* *
362
+ * @brief Initialize the server %BLE enevironment.
363
+ *
364
+ */
365
+
366
+ void BLE::initServer () {
367
+ esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
368
+ esp_err_t errRc = esp_bt_controller_init (&bt_cfg);
369
+ if (errRc != ESP_OK) {
370
+ ESP_LOGE (tag, " esp_bt_controller_init: rc=%d %s" , errRc, espToString (errRc));
371
+ return ;
372
+ }
373
+
374
+
375
+ errRc = esp_bt_controller_enable (ESP_BT_MODE_BTDM);
376
+ if (errRc != ESP_OK) {
377
+ ESP_LOGE (tag, " esp_bt_controller_enable: rc=%d %s" , errRc, espToString (errRc));
378
+ return ;
379
+ }
380
+
381
+ errRc = esp_bluedroid_init ();
382
+ if (errRc != ESP_OK) {
383
+ ESP_LOGE (tag, " esp_bluedroid_init: rc=%d %s" , errRc, espToString (errRc));
384
+ return ;
385
+ }
317
386
387
+ errRc = esp_bluedroid_enable ();
388
+ if (errRc != ESP_OK) {
389
+ ESP_LOGE (tag, " esp_bluedroid_enable: rc=%d %s" , errRc, espToString (errRc));
390
+ return ;
391
+ }
392
+
393
+ errRc = esp_ble_gap_register_callback (gap_event_handler);
394
+ if (errRc != ESP_OK) {
395
+ ESP_LOGE (tag, " esp_ble_gap_register_callback: rc=%d %s" , errRc, espToString (errRc));
396
+ return ;
397
+ }
398
+
399
+ errRc = esp_ble_gatts_register_callback (gatt_server_event_handler);
400
+ if (errRc != ESP_OK) {
401
+ ESP_LOGE (tag, " esp_ble_gatts_register_callback: rc=%d %s" , errRc, espToString (errRc));
402
+ return ;
403
+ }
404
+ }
318
405
/* *
319
- * @brief Initialize the %BLE environment.
406
+ * @brief Initialize the client %BLE environment.
320
407
*/
321
- void BLE::init () {
322
- esp_bt_controller_init ();
408
+ void BLE::initClient () {
409
+ esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
410
+ esp_err_t errRc = esp_bt_controller_init (&bt_cfg);
411
+ if (errRc != ESP_OK) {
412
+ ESP_LOGE (tag, " esp_bt_controller_init: rc=%d %s" , errRc, espToString (errRc));
413
+ return ;
414
+ }
415
+
323
416
324
- esp_err_t errRc = esp_bt_controller_enable (ESP_BT_MODE_BTDM);
417
+ errRc = esp_bt_controller_enable (ESP_BT_MODE_BTDM);
325
418
if (errRc != ESP_OK) {
326
419
ESP_LOGE (tag, " esp_bt_controller_enable: rc=%d %s" , errRc, espToString (errRc));
327
420
return ;
@@ -345,7 +438,7 @@ void BLE::init() {
345
438
return ;
346
439
}
347
440
348
- errRc = esp_ble_gattc_register_callback (gatt_event_handler );
441
+ errRc = esp_ble_gattc_register_callback (gatt_client_event_handler );
349
442
if (errRc != ESP_OK) {
350
443
ESP_LOGE (tag, " esp_ble_gattc_register_callback: rc=%d %s" , errRc, espToString (errRc));
351
444
return ;
0 commit comments