8000 sync 2017-07-10 · copercini/esp32-snippets@88f59b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 88f59b5

Browse files
committed
sync 2017-07-10
1 parent 5715cd7 commit 88f59b5

16 files changed

+781
-625
lines changed

Documentation/BLE C++ Guide.pdf

90.8 KB
Binary file not shown.

cpp_utils/BLE.cpp

Lines changed: 24 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <bt.h> // ESP32 BLE
1313
#include <esp_bt_main.h> // ESP32 BLE
1414
#include <esp_gap_ble_api.h> // ESP32 BLE
15-
#include <esp_gattc_api.h> // ESP32 BLE
15+
// ESP32 BLE
1616
#include <esp_gatts_api.h> // ESP32 BLE
1717
#include <esp_err.h> // ESP32 ESP-IDF
1818
#include <esp_log.h> // ESP32 ESP-IDF
@@ -23,29 +23,16 @@
2323
#include "BLE.h"
2424
#include "BLEClient.h"
2525
#include "BLEUtils.h"
26-
#include "BLEXXXCharacteristic.h"
2726
#include "GeneralUtils.h"
2827

2928
static char LOG_TAG[] = "BLE";
3029

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-
4530
BLEServer *BLE::m_bleServer = nullptr;
4631
BLEScan *BLE::m_pScan = nullptr;
4732
BLEClient *BLE::m_pClient = nullptr;
4833

34+
#include <esp_gattc_api.h>
35+
4936
BLE::BLE() {
5037
}
5138

@@ -57,21 +44,7 @@ BLE::~BLE() {
5744
BLEClient* BLE::createClient() {
5845
m_pClient = new BLEClient();
5946
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
7548

7649

7750
/**
@@ -81,19 +54,19 @@ void BLE::dumpDevices() {
8154
* @param [in] gatts_if
8255
* @param [in] param
8356
*/
84-
static void gatt_server_event_handler(
57+
void BLE::gattServerEventHandler(
8558
esp_gatts_cb_event_t event,
8659
esp_gatt_if_t gatts_if,
8760
esp_ble_gatts_cb_param_t *param
8861
) {
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",
9063
gatts_if,
9164
BLEUtils::gattServerEventTypeToString(event).c_str());
9265
BLEUtils::dumpGattServerEvent(event, gatts_if, param);
9366
if (BLE::m_bleServer != nullptr) {
9467
BLE::m_bleServer->handleGATTServerEvent(event, gatts_if, param);
9568
}
96-
} // gatt_server_event_handler
69+
} // gattServerEventHandler
9770

9871

9972
/**
@@ -109,93 +82,16 @@ static void gatt_server_event_handler(
10982
* @param [in] gattc_if
11083
* @param [in] param
11184
*/
112-
static void gatt_client_event_handler(
85+
void BLE::gattClientEventHandler(
11386
esp_gattc_cb_event_t event,
11487
esp_gatt_if_t gattc_if,
11588
esp_ble_gattc_cb_param_t *param) {
11689

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",
11891
gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str());
11992
BLEUtils::dumpGattClientEvent(event, gattc_if, param);
12093

12194
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-
19995
default: {
20096
break;
20197
}
@@ -206,13 +102,13 @@ static void gatt_client_event_handler(
206102
BLE::m_pClient->gattClientEventHandler(event, gattc_if, param);
207103
}
208104

209-
} // gatt_event_handler
105+
} // gattClientEventHandler
210106

211107

212108
/**
213109
* @brief Handle GAP events.
214110
*/
215-
static void gap_event_handler(
111+
void BLE::gapEventHandler(
216112
esp_gap_ble_cb_event_t event,
217113
esp_ble_gap_cb_param_t *param) {
218114

@@ -236,18 +132,10 @@ static void gap_event_handler(
236132
BLE::m_bleServer->handleGAPEvent(event, param);
237133
}
238134

239-
if (BLE::getScan() != nullptr) {
135+
if (BLE::m_pScan != nullptr) {
240136
BLE::getScan()->gapEventHandler(event, param);
241137
}
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
251139

252140

253141
/**
@@ -281,13 +169,13 @@ std::map<std::string, BLEClient> getDevices() {
281169
return;
282170
}
283171

284-
errRc = esp_ble_gap_register_callback(gap_event_handler);
172+
errRc = esp_ble_gap_register_callback(BLE::gapEventHandler);
285173
if (errRc != ESP_OK) {
286174
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
287175
return;
288176
}
289177

290-
errRc = esp_ble_gatts_register_callback(gatt_server_event_handler);
178+
errRc = esp_ble_gatts_register_callback(BLE::gattServerEventHandler);
291179
if (errRc != ESP_OK) {
292180
ESP_LOGE(LOG_TAG, "esp_ble_gatts_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
293181
return;
@@ -307,7 +195,7 @@ std::map<std::string, BLEClient> getDevices() {
307195
};
308196

309197
return;
310-
}
198+
} // initServer
311199

312200

313201
/**
@@ -321,7 +209,6 @@ void BLE::initClient() {
321209
return;
322210
}
323211

324-
325212
errRc = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
326213
if (errRc != ESP_OK) {
327214
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -340,22 +227,25 @@ void BLE::initClient() {
340227
return;
341228
}
342229

343-
errRc = esp_ble_gap_register_callback(gap_event_handler);
230+
errRc = esp_ble_gap_register_callback(BLE::gapEventHandler);
344231
if (errRc != ESP_OK) {
345232
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
346233
return;
347234
}
348235

349-
errRc = esp_ble_gattc_register_callback(gatt_client_event_handler);
236+
errRc = esp_ble_gattc_register_callback(BLE::gattClientEventHandler);
350237
if (errRc != ESP_OK) {
351238
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
352239
return;
353240
}
354241

355-
g_eventGroup = xEventGroupCreate();
356-
xEventGroupClearBits(g_eventGroup, 0xff);
357-
} // init
242+
} // initClient
243+
358244

245+
/**
246+
* @brief Retrieve the Scan object that we use for scanning.
247+
* @return The scanning object reference.
248+
*/
359249
BLEScan* BLE::getScan() {
360250
if (m_pScan == nullptr) {
361251
m_pScan = new BLEScan();

cpp_utils/BLE.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,30 @@ class BLE {
2727
BLE();
2828
virtual ~BLE();
2929
static void dumpDevices();
30-
static std::map<std::string, BLEClient> getDevices();
3130
static BLEClient *createClient();
3231

3332
static void initClient();
3433
static void initServer(std::string deviceName);
3534
//static void scan(int duration, esp_ble_scan_type_t scan_type = BLE_SCAN_TYPE_PASSIVE);
36-
static BLEScan *getScan();
37-
static esp_gatt_if_t getGattcIF();
35+
static BLEScan *getScan();
3836
static BLEServer *m_bleServer;
3937
static BLEScan *m_pScan;
4038
static BLEClient *m_pClient;
39+
40+
private:
41+
static esp_gatt_if_t getGattcIF();
42+
43+
static void gattClientEventHandler(
44+
esp_gattc_cb_event_t event,
45+
esp_gatt_if_t gattc_if,
46+
esp_ble_gattc_cb_param_t *param);
47+
static void gattServerEventHandler(
48+
esp_gatts_cb_event_t event,
49+
esp_gatt_if_t gatts_if,
50+
esp_ble_gatts_cb_param_t *param);
51+
static void gapEventHandler(
52+
esp_gap_ble_cb_event_t event,
53+
esp_ble_gap_cb_param_t *param);
4154
}; // class BLE
4255

4356
#endif // CONFIG_BT_ENABLED

cpp_utils/BLEAdvertisedDevice.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
2727
m_name = "";
2828
m_rssi = -9999;
2929
m_txPower = 0;
30+
m_pScan = nullptr;
3031

3132
m_haveAppearance = false;
3233
m_haveManufacturerData = false;
3334
m_haveName = false;
3435
m_haveRSSI = false;
3536
m_haveServiceUUID = false;
3637
m_haveTXPower = false;
38+
3739
} // BLEAdvertisedDevice
3840

3941

@@ -169,10 +171,14 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t *payload) {
169171
if (length != 0) { // A length of 0 indicate that we have reached the end.
170172
ad_type = *payload;
171173
payload++;
174+
length--;
175+
176+
char *pHex = BLEUtils::buildHexData(nullptr, payload, length);
177+
ESP_LOGD(LOG_TAG, "Type: 0x%.2x (%s), length: %d, data: %s",
178+
ad_type, BLEUtils::advTypeToString(ad_type), length, pHex);
179+
free(pHex);
172180

173-
ESP_LOGD(LOG_TAG, "Type: 0x%.2x (%s), length: %d", ad_type, BLEUtils::advTypeToString(ad_type), length);
174181

175-
length--;
176182

177183
switch(ad_type) {
178184
case ESP_BLE_AD_TYPE_NAME_CMPL: { // Adv Data Type: 0x09
@@ -216,12 +222,12 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t *payload) {
216222
} // ESP_BLE_AD_TYPE_32SRV_PART
217223

218224
case ESP_BLE_AD_TYPE_128SRV_CMPL: { // Adv Data Type: 0x07
219-
setServiceUUID(BLEUUID(payload, 16));
225+
setServiceUUID(BLEUUID(payload, 16, false));
220226
break;
221227
} // ESP_BLE_AD_TYPE_128SRV_CMPL
222228

223229
case ESP_BLE_AD_TYPE_128SRV_PART: { // Adv Data Type: 0x06
224-
setServiceUUID(BLEUUID(payload, 16));
230+
setServiceUUID(BLEUUID(payload, 16, false));
225231
break;
226232
} // ESP_BLE_AD_TYPE_128SRV_PART
227233

0 commit comments

Comments
 (0)
0