8000 Mon May 8 13:24:20 CDT 2017 · programmer131/esp32-snippets@1e58455 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e58455

Browse files
author
kolban
committed
Mon May 8 13:24:20 CDT 2017
1 parent 2e1e098 commit 1e58455

File tree

10 files changed

+362
-53
lines changed

10 files changed

+362
-53
lines changed

cpp_utils/BLE.cpp

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <esp_bt_main.h> // ESP32 BLE
1414
#include <esp_gap_ble_api.h> // ESP32 BLE
1515
#include <esp_gattc_api.h> // ESP32 BLE
16+
#include <esp_gatts_api.h> // ESP32 BLE
1617
#include <esp_err.h> // ESP32 ESP-IDF
1718
#include <esp_log.h> // ESP32 ESP-IDF
1819
#include <map> // Part of C++ STL
@@ -43,6 +44,8 @@ static EventGroupHandle_t g_eventGroup;
4344
*/
4445
static std::map<ble_address, BLEDevice> g_devices;
4546

47+
BLEServer *BLE::m_server;
48+
4649
BLE::BLE() {
4750
}
4851

@@ -166,24 +169,48 @@ static void dump_adv_payload(uint8_t *payload) {
166169
*/
167170

168171

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+
169192

170193
/**
171-
* @brief Handle GATT events.
194+
* @brief Handle GATT client events.
172195
*
173-
* Handler for the GATT events.
196+
* Handler for the GATT client events.
174197
* * `ESP_GATTC_OPEN_EVT` – Invoked when a connection is opened.
175198
* * `ESP_GATTC_PREP_WRITE_EVT` – Response to write a characteristic.
176199
* * `ESP_GATTC_READ_CHAR_EVT` – Response to read a characteristic.
177200
* * `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
178205
*/
179-
static void gatt_event_handler(
206+
static void gatt_client_event_handler(
180207
esp_gattc_cb_event_t event,
181208
esp_gatt_if_t gattc_if,
182209
esp_ble_gattc_cb_param_t *param) {
183210

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);
187214

188215
switch(event) {
189216
case ESP_GATTC_OPEN_EVT: {
@@ -257,6 +284,7 @@ static void gatt_event_handler(
257284
default:
258285
break;
259286
}
287+
260288
} // gatt_event_handler
261289

262290

@@ -277,6 +305,19 @@ static void gap_event_handler(
277305
ESP_LOGD(tag, "status: %d", param->scan_start_cmpl.status);
278306
}
279307

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+
280321
else if (event == ESP_GAP_BLE_SCAN_RESULT_EVT) {
281322
BLEDevice device;
282323

@@ -304,6 +345,9 @@ static void gap_event_handler(
304345
ESP_LOGD(tag, "Unhandled search_evt type!");
305346
}
306347
} // ESP_GAP_BLE_SCAN_RESULT_EVT
348+
if (BLE::m_server != nullptr) {
349+
BLE::m_server->handleGAPEvent(event, param);
350+
}
307351
} // gap_event_handler
308352

309353

@@ -314,14 +358,63 @@ std::map<ble_address, BLEDevice> getDevices() {
314358
return g_devices;
315359
} // getDevices
316360

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+
}
317386

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+
}
318405
/**
319-
* @brief Initialize the %BLE environment.
406+
* @brief Initialize the client %BLE environment.
320407
*/
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+
323416

324-
esp_err_t errRc = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
417+
errRc = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
325418
if (errRc != ESP_OK) {
326419
ESP_LOGE(tag, "esp_bt_controller_enable: rc=%d %s", errRc, espToString(errRc));
327420
return;
@@ -345,7 +438,7 @@ void BLE::init() {
345438
return;
346439
}
347440

348-
errRc = esp_ble_gattc_register_callback(gatt_event_handler);
441+
errRc = esp_ble_gattc_register_callback(gatt_client_event_handler);
349442
if (errRc != ESP_OK) {
350443
ESP_LOGE(tag, "esp_ble_gattc_register_callback: rc=%d %s", errRc, espToString(errRc));
351444
return;

cpp_utils/BLE.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <esp_gattc_api.h> // ESP32 BLE
1414
#include <map> // Part of C++ STL
1515

16+
#include "BLEServer.h"
1617
#include "BLEDevice.h"
1718
#include "BLEUtils.h"
1819
/**
@@ -25,9 +26,11 @@ class BLE {
2526
static void dumpDevices();
2627
static std::map<ble_address, BLEDevice> getDevices();
2728

28-
static void init();
29+
static void initClient();
30+
static void initServer();
2931
static void scan(int duration, esp_ble_scan_type_t scan_type = BLE_SCAN_TYPE_PASSIVE);
3032
static esp_gatt_if_t getGattcIF();
33+
static BLEServer *m_server;
3134
}; // class BLE
3235

3336
#endif // CONFIG_BT_ENABLED

0 commit comments

Comments
 (0)
0