10000 Sync · programmer131/esp32-snippets@18fa8fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 18fa8fe

Browse files
committed
Sync
1 parent 0480057 commit 18fa8fe

15 files changed

+485
-199
lines changed

cpp_utils/BLE.cpp

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -296,56 +296,53 @@ static void gap_event_handler(
296296
esp_gap_ble_cb_event_t event,
297297
esp_ble_gap_cb_param_t *param) {
298298

299-
ESP_LOGD(LOG_TAG, "Received a GAP event: %s", bt_event_type_to_string(event).c_str());
299+
BLEUtils::dumpGapEvent(event, param);
300300

301-
if (event == ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT) {
302-
ESP_LOGD(LOG_TAG, "status: %d", param->scan_param_cmpl.status);
303-
} // ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT
304-
305-
else if (event == ESP_GAP_BLE_SCAN_START_COMPLETE_EVT) {
306-
ESP_LOGD(LOG_TAG, "status: %d", param->scan_start_cmpl.status);
307-
}
301+
switch(event) {
302+
case ESP_GAP_BLE_SCAN_RESULT_EVT: {
303+
BLEDevice device;
304+
305+
ESP_LOGD(LOG_TAG, "search_evt: %s", bt_gap_search_event_type_to_string(param->scan_rst.search_evt).c_str());
306+
307+
if (param->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT) {
308+
//ESP_LOGD(tag, "num_resps: %d", param->scan_rst.num_resps);
309+
//BLE::dumpDevices();
310+
xEventGroupSetBits(g_eventGroup, EVENT_GROUP_SCAN_COMPLETE);
311+
}
312+
else if (param->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) {
313+
//ESP_LOGD(tag, "device type: %s", bt_dev_type_to_string(param->scan_rst.dev_type));
314+
//ESP_LOGD(tag, "device address (bda): %02x:%02x:%02x:%02x:%02x:%02x", BT_BD_ADDR_HEX(param->scan_rst.bda));
315+
//ESP_LOGD(tag, "rssi: %d", param->scan_rst.rssi);
316+
//ESP_LOGD(tag, "addr_type: %s", bt_addr_t_to_string(param->scan_rst.ble_addr_type));
317+
//ESP_LOGD(tag, "flag: %d", param->scan_rst.flag);
318+
device.setAddress(std::string((char *)param->scan_rst.bda, 6));
319+
device.setRSSI(param->scan_rst.rssi);
320+
device.setAdFlag(param->scan_rst.flag);
321+
//device.dump();
322+
device.parsePayload((uint8_t *)param->scan_rst.ble_adv);
323+
g_devices.insert(std::pair<std::string,BLEDevice>(device.getAddress(),device));
324+
//dump_adv_payload(param->scan_rst.ble_adv);
325+
} else {
326+
ESP_LOGD(LOG_TAG, "Unhandled search_evt type!");
327+
}
328+
break;
329+
} // ESP_GAP_BLE_SCAN_RESULT_EVT
330+
331+
case ESP_GAP_BLE_SEC_REQ_EVT: {
332+
esp_err_t errRc = ::esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true);
333+
if (errRc != ESP_OK) {
334+
ESP_LOGE(LOG_TAG, "esp_ble_gap_security_rsp: rc=%d %s", errRc, espToString(errRc));
335+
}
336+
break;
337+
}
308338

309-
else if (event == ESP_GAP_BLE_AUTH_CMPL_EVT) {
310-
// key is a 16 byte value
311-
312-
ESP_LOGD(LOG_TAG, "[bd_addr: %s, key_present: %d, key: ***, key_type: %d, success: %d, fail_reason: %d, addr_type: ***, dev_type: %s]",
313-
BLEUtils::addressToString(param->ble_security.auth_cmpl.bd_addr).c_str(),
314-
param->ble_security.auth_cmpl.key_present,
315-
param->ble_security.auth_cmpl.key_type,
316-
param->ble_security.auth_cmpl.success,
317-
param->ble_security.auth_cmpl.fail_reason,
318-
BLEUtils::devTypeToString(param->ble_security.auth_cmpl.dev_type).c_str()
319-
);
320-
}
339+
default: {
340+
break;
341+
}
342+
} // switch
321343

322-
else if (event == ESP_GAP_BLE_SCAN_RESULT_EVT) {
323-
BLEDevice device;
324344

325-
ESP_LOGD(LOG_TAG, "search_evt: %s", bt_gap_search_event_type_to_string(param->scan_rst.search_evt).c_str());
326345

327-
if (param->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT) {
328-
//ESP_LOGD(tag, "num_resps: %d", param->scan_rst.num_resps);
329-
//BLE::dumpDevices();
330-
xEventGroupSetBits(g_eventGroup, EVENT_GROUP_SCAN_COMPLETE);
331-
}
332-
else if (param->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) {
333-
//ESP_LOGD(tag, "device type: %s", bt_dev_type_to_string(param->scan_rst.dev_type));
334-
//ESP_LOGD(tag, "device address (bda): %02x:%02x:%02x:%02x:%02x:%02x", BT_BD_ADDR_HEX(param->scan_rst.bda));
335-
//ESP_LOGD(tag, "rssi: %d", param->scan_rst.rssi);
336-
//ESP_LOGD(tag, "addr_type: %s", bt_addr_t_to_string(param->scan_rst.ble_addr_type));
337-
//ESP_LOGD(tag, "flag: %d", param->scan_rst.flag);
338-
device.setAddress(std::string((char *)param->scan_rst.bda, 6));
339-
device.setRSSI(param->scan_rst.rssi);
340-
device.setAdFlag(param->scan_rst.flag);
341-
//device.dump();
342-
device.parsePayload((uint8_t *)param->scan_rst.ble_adv);
343-
g_devices.insert(std::pair<std::string,BLEDevice>(device.getAddress(),device));
344-
//dump_adv_payload(param->scan_rst.ble_adv);
345-
} else {
346-
ESP_LOGD(LOG_TAG, "Unhandled search_evt type!");
347-
}
348-
} // ESP_GAP_BLE_SCAN_RESULT_EVT
349346
if (BLE::m_bleServer != nullptr) {
350347
BLE::m_bleServer->handleGAPEvent(event, param);
351348
}
@@ -409,6 +406,13 @@ BLEServer *BLE::initServer(std::string deviceName) {
409406
return nullptr;
410407
};
411408

409+
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
410+
errRc = ::esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
411+
if (errRc != ESP_OK) {
412+
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_security_param: rc=%d %s", errRc, espToString(errRc));
413+
return nullptr;
414+
};
415+
412416
m_bleServer = new BLEServer(0);
413417
m_bleServer->registerApp();
414418
return m_bleServer;

cpp_utils/BLECharacteristic.cpp

Lines changed: 102 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@
1313
#include <esp_log.h>
1414
#include <esp_err.h>
1515
#include "BLECharacteristic.h"
16+
#include "BLEService.h"
1617

1718
static char LOG_TAG[] = "BLECharacteristic";
1819

1920
extern "C" {
2021
char *espToString(esp_err_t value);
2122
}
2223

24+
/**
25+
* @brief Construct a characteristic
26+
* @param [in] uuid - UUID for the characteristic.
27+
* @param [in] properties - Properties for the characteristic.
28+
*/
2329
BLECharacteristic::BLECharacteristic(BLEUUID uuid, uint32_t properties) {
2430
m_bleUUID = uuid;
2531
m_value.attr_value = (uint8_t *)malloc(ESP_GATT_MAX_ATTR_LEN);
@@ -84,25 +90,27 @@ void BLECharacteristic::handleGATTServerEvent(
8490
esp_gatt_if_t gatts_if,
8591
esp_ble_gatts_cb_param_t *param) {
8692
switch(event) {
93+
8794
// ESP_GATTS_WRITE_EVT - A request to write the value of a characteristic has arrived.
8895
//
8996
// write:
90-
// - uint16_t conn_id
91-
// - uint16_t trans_id
97+
// - uint16_t conn_id
98+
// - uint16_t trans_id
9299
// - esp_bd_addr_t bda
93-
// - uint16_t handle
94-
// - uint16_t offset
95-
// - bool need_rsp
96-
// - bool is_prep
97-
// - uint16_t len
98-
// - uint8_t *value
100+
// - uint16_t handle
101+
// - uint16_t offset
102+
// - bool need_rsp
103+
// - bool is_prep
104+
// - uint16_t len
105+
// - uint8_t *value
106+
//
99107
case ESP_GATTS_WRITE_EVT: {
100108
if (param->write.handle == m_handle) {
101109
setValue(param->write.value, param->write.len);
102110
esp_gatt_rsp_t rsp;
103-
rsp.attr_value.len = getLength();
104-
rsp.attr_value.handle = m_handle;
105-
rsp.attr_value.offset = 0;
111+
rsp.attr_value.len = getLength();
112+
rsp.attr_value.handle = m_handle;
113+
rsp.attr_value.offset = 0;
106114
rsp.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
107115
memcpy(rsp.attr_value.value, getValue(), rsp.attr_value.len);
108116
esp_err_t errRc = ::esp_ble_gatts_send_response(
@@ -117,23 +125,23 @@ void BLECharacteristic::handleGATTServerEvent(
117125
// ESP_GATTS_READ_EVT - A request to read the value of a characteristic has arrived.
118126
//
119127
// read:
120-
// - uint16_t conn_id
121-
// - uint32_t trans_id
128+
// - uint16_t conn_id
129+
// - uint32_t trans_id
122130
// - esp_bd_addr_t bda
123-
// - uint16_t handle
124-
// - uint16_t offset
125-
// - bool is_long
126-
// - bool need_rsp
131+
// - uint16_t handle
132+
// - uint16_t offset
133+
// - bool is_long
134+
// - bool need_rsp
127135
//
128136< F438 code class="diff-text syntax-highlighted-line">
case ESP_GATTS_READ_EVT: {
129-
ESP_LOGD(LOG_TAG, "- Testing: %d == %d", param->read.handle, m_handle);
137+
ESP_LOGD(LOG_TAG, "- Testing: 0x%.2x == 0x%.2x", param->read.handle, m_handle);
130138
if (param->read.handle == m_handle) {
131139
ESP_LOGD(LOG_TAG, "Sending a response (esp_ble_gatts_send_response)");
132140
if (param->read.need_rsp) {
133141
esp_gatt_rsp_t rsp;
134-
rsp.attr_value.len = getLength();
135-
rsp.attr_value.handle = param->read.handle;
136-
rsp.attr_value.offset = 0;
142+
rsp.attr_value.len = getLength();
143+
rsp.attr_value.handle = param->read.handle;
144+
rsp.attr_value.offset = 0;
137145
rsp.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
138146
memcpy(rsp.attr_value.value, getValue(), rsp.attr_value.len);
139147
esp_err_t errRc = ::esp_ble_gatts_send_response(
@@ -145,10 +153,21 @@ void BLECharacteristic::handleGATTServerEvent(
145153
} // ESP_GATTS_READ_EVT
146154
break;
147155
} // ESP_GATTS_READ_EVT
156+
148157
default: {
149158
break;
150-
}
151-
}// switch event
159+
} // default
160+
161+
} // switch event
162+
163+
// Give each of the descriptors associated with this characteristic the opportunity to handle the
164+
// event.
165+
BLEDescriptor *pDescriptor = m_descriptorMap.getFirst();
166+
while(pDescriptor != nullptr) {
167+
pDescriptor->handleGATTServerEvent(event, gatts_if, param);
168+
pDescriptor = m_descriptorMap.getNext();
169+
}
170+
152171
} // handleGATTServerEvent
153172

154173

@@ -158,7 +177,7 @@ void BLECharacteristic::handleGATTServerEvent(
158177
* @return N/A
159178
*/
160179
void BLECharacteristic::setBroadcastProperty(bool value) {
161-
ESP_LOGD(LOG_TAG, "setBroadcastProperty(%d)", value);
180+
//ESP_LOGD(LOG_TAG, "setBroadcastProperty(%d)", value);
162181
if (value) {
163182
m_properties |= ESP_GATT_CHAR_PROP_BIT_BROADCAST;
164183
} else {
@@ -175,7 +194,7 @@ void BLECharacteristic::setHandle(uint16_t handle) {
175194

176195

177196
void BLECharacteristic::setIndicateProperty(bool value) {
178-
ESP_LOGD(LOG_TAG, "setIndicateProperty(%d)", value);
197+
//ESP_LOGD(LOG_TAG, "setIndicateProperty(%d)", value);
179198
if (value) {
180199
m_properties |= ESP_GATT_CHAR_PROP_BIT_INDICATE;
181200
} else {
@@ -185,7 +204,7 @@ void BLECharacteristic::setIndicateProperty(bool value) {
185204

186205

187206
void BLECharacteristic::setNotifyProperty(bool value) {
188-
ESP_LOGD(LOG_TAG, "setNotifyProperty(%d)", value);
207+
//ESP_LOGD(LOG_TAG, "setNotifyProperty(%d)", value);
189208
if (value) {
190209
m_properties |= ESP_GATT_CHAR_PROP_BIT_NOTIFY;
191210
} else {
@@ -195,7 +214,7 @@ void BLECharacteristic::setNotifyProperty(bool value) {
195214

196215

197216
void BLECharacteristic::setReadProperty(bool value) {
198-
ESP_LOGD(LOG_TAG, "setReadProperty(%d)", value);
217+
//ESP_LOGD(LOG_TAG, "setReadProperty(%d)", value);
199218
if (value) {
200219
m_properties |= ESP_GATT_CHAR_PROP_BIT_READ;
201220
} else {
@@ -225,7 +244,7 @@ void BLECharacteristic::setValue(std::string value) {
225244

226245

227246
void BLECharacteristic::setWriteNoResponseProperty(bool value) {
228-
ESP_LOGD(LOG_TAG, "setWriteNoResponseProperty(%d)", value);
247+
//ESP_LOGD(LOG_TAG, "setWriteNoResponseProperty(%d)", value);
229248
if (value) {
230249
m_properties |= ESP_GATT_CHAR_PROP_BIT_WRITE_NR;
231250
} else {
@@ -235,7 +254,7 @@ void BLECharacteristic::setWriteNoResponseProperty(bool value) {
235254

236255

237256
void BLECharacteristic::setWriteProperty(bool value) {
238-
ESP_LOGD(LOG_TAG, "setWriteProperty(%d)", value);
257+
//ESP_LOGD(LOG_TAG, "setWriteProperty(%d)", value);
239258
if (value) {
240259
m_properties |= ESP_GATT_CHAR_PROP_BIT_WRITE;
241260
} else {
@@ -252,17 +271,63 @@ std::string BLECharacteristic::toString() {
252271
std::stringstream stringstream;
253272
stringstream << std::hex << std::setfill('0');
254273
stringstream << "UUID: " << m_bleUUID.toString() + ", handle: 0x" << std::setw(2) << m_handle;
255-
stringstream <<
256-
((m_properties & ESP_GATT_CHAR_PROP_BIT_READ)?"Read ":"") <<
257-
((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE)?"Write ":"") <<
258-
((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE_NR)?"WriteNoResponse ":"") <<
259-
((m_properties & ESP_GATT_CHAR_PROP_BIT_BROADCAST)?"Broadcast ":"") <<
260-
((m_properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)?"Notify ":"") <<
261-
((m_properties & ESP_GATT_CHAR_PROP_BIT_INDICATE)?"Indicate ":"") <<
262-
"\n";
274+
stringstream << " " <<
275+
((m_properties & ESP_GATT_CHAR_PROP_BIT_READ)?"Read ":"") <<
276+
((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE)?"Write ":"") <<
277+
((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE_NR)?"WriteNoResponse ":"") <<
278+
((m_properties & ESP_GATT_CHAR_PROP_BIT_BROADCAST)?"Broadcast ":"") <<
279+
((m_properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)?"Notify ":"") <<
280+
((m_properties & ESP_GATT_CHAR_PROP_BIT_INDICATE)?"Indicate ":"");
263281
return stringstream.str();
264282
} // toString
265283

266284
BLEService* BLECharacteristic::getService() {
267285
return m_pService;
268286
}
287+
288+
289+
/**
290+
* @brief Register a new characteristic with the ESP runtime.
291+
* @param [in] pService The service with which to associate this characteristic.
292+
*/
293+
void BLECharacteristic::executeCreate(BLEService* pService) {
294+
ESP_LOGD(LOG_TAG, ">> executeCreate()");
295+
296+
if (m_handle != 0) {
297+
ESP_LOGE(LOG_TAG, "Characteristic already has a handle.");
298+
return;
299+
}
300+
301+
m_pService = pService; // Save the service for this characteristic.
302+
303+
ESP_LOGD(LOG_TAG, "Registering characteristic (esp_ble_gatts_add_char): uuid: %s, service: %s",
304+
getUUID().toString().c_str(),
305+
m_pService->toString().c_str());
306+
307+
//m_serializeMutex.take("addCharacteristic"); // Take the mutex, released by event ESP_GATTS_ADD_CHAR_EVT
308+
esp_err_t errRc = ::esp_ble_gatts_add_char(
309+
m_pService->getHandle(),
310+
getUUID().getNative(),
311+
(esp_gatt_perm_t)(ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE),
312+
getProperties(),
313+
&m_value,
314+
NULL);
315+
316+
if (errRc != ESP_OK) {
317+
ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_add_char: rc=%d %s", errRc, espToString(errRc));
318+
return;
319+
}
320+
321+
// Now that we have registered the characteristic, we must also register all the descriptors associated with this
322+
// characteristic. We iterate through each of those and invoke the registration call to register them with the
323+
// ESP environment.
324+
325+
BLEDescriptor *pDescriptor = m_descriptorMap.getFirst();
326+
327+
while (pDescriptor != nullptr) {
328+
pDescriptor->executeCreate(this);
329+
pDescriptor = m_descriptorMap.getNext();
330+
} // End while
331+
332+
ESP_LOGD(LOG_TAG, "<< executeCreate()");
333+
} // executeCreate

cpp_utils/BLECharacteristic.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@ class BLECharacteristic {
5050
friend class BLEService;
5151
friend class BLEDescriptor;
5252
friend class BLECharacteristicMap;
53-
BLEUUID m_bleUUID;
53+
BLEUUID m_bleUUID;
5454
esp_gatt_char_prop_t m_properties;
5555
esp_attr_value_t m_value;
5656
uint16_t m_handle;
5757
BLEService *m_pService;
58-
BLEService *getService();
59-
esp_gatt_char_prop_t getProperties();
60-
void setHandle(uint16_t handle);
6158
BLEDescriptorMap m_descriptorMap;
59+
60+
void executeCreate(BLEService *pService);
6261
uint16_t getHandle();
62+
esp_gatt_char_prop_t getProperties();
63+
BLEService *getService();
64+
void setHandle(uint16_t handle);
6365
};
6466

6567
#endif /* COMPONENTS_CPP_UTILS_BLECHARACTERISTIC_H_ */

0 commit comments

Comments
 (0)
0