8000 sync 2017-07-03 · adiorio/esp32-snippets@e311f0a · GitHub
[go: up one dir, main page]

Skip to content

Commit e311f0a

Browse files
committed
sync 2017-07-03
1 parent c4d3284 commit e311f0a

21 files changed

+1155
-799
lines changed

cpp_utils/BLE.cpp

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@
2020
#include <sstream>
2121
#include <iomanip>
2222

23-
#include "BLEUtils.h"
2423
#include "BLE.h"
25-
#include "BLEDevice.h"
24+
#include "BLERemoteDevice.h"
25+
#include "BLEUtils.h"
2626
#include "BLEXXXCharacteristic.h"
27+
#include "GeneralUtils.h"
2728

2829
static char LOG_TAG[] = "BLE";
2930

30-
extern "C" {
31-
char *espToString(esp_err_t value);
32-
}
3331

3432
static EventGroupHandle_t g_eventGroup;
3533
#define EVENT_GROUP_SCAN_COMPLETE (1<<0)
@@ -42,7 +40,7 @@ static EventGroupHandle_t g_eventGroup;
4240
* Note that this address is binary and should not be directly printed. The value of the map is
4341
* the BLEDevice object that this device represents.
4442
*/
45-
static std::map<ble_address, BLEDevice> g_devices;
43+
static std::map<std::string, BLERemoteDevice> g_devices;
4644

4745
BLEServer *BLE::m_bleServer;
4846

@@ -215,7 +213,7 @@ static void gatt_client_event_handler(
215213

216214
switch(event) {
217215
case ESP_GATTC_OPEN_EVT: {
218-
BLEDevice *pDevice = BLEUtils::findByAddress(std::string((char *)param->open.remote_bda, 6));
216+
BLERemoteDevice *pDevice = BLEUtils::findByAddress(std::string((char *)param->open.remote_bda, 6));
219217
BLEUtils::registerByConnId(param->open.conn_id, pDevice);
220218
pDevice->dump();
221219
pDevice->onConnected(param->open.status);
@@ -232,13 +230,13 @@ static void gatt_client_event_handler(
232230
* * bool is_primary
233231
*/
234232
case ESP_GATTC_SEARCH_RES_EVT: {
235-
BLEDevice *pDevice = BLEUtils::findByConnId(param->search_res.conn_id);
233+
BLERemoteDevice *pDevice = BLEUtils::findByConnId(param->search_res.conn_id);
236234
pDevice->addService(param->search_res.srvc_id);
237235
break;
238236
}
239237

240238
case ESP_GATTC_SEARCH_CMPL_EVT: {
241-
BLEDevice *pDevice = BLEUtils::findByConnId(param->search_cmpl.conn_id);
239+
BLERemoteDevice *pDevice = BLEUtils::findByConnId(param->search_cmpl.conn_id);
242240
pDevice->onSearchComplete();
243241
break;
244242
}
@@ -253,7 +251,7 @@ static void gatt_client_event_handler(
253251
*/
254252
case ESP_GATTC_GET_CHAR_EVT: {
255253
if (param->get_char.status == ESP_GATT_OK) {
256-
BLEDevice *pDevice = BLEUtils::findByConnId(param->get_char.conn_id);
254+
BLERemoteDevice *pDevice = BLEUtils::findByConnId(param->get_char.conn_id);
257255
BLECharacteristicXXX characteristic(param->get_char.conn_id,
258256
param->get_char.srvc_id, param->get_char.char_id, param->get_char.char_prop);
259257
pDevice->onCharacteristic(characteristic);
@@ -275,7 +273,7 @@ static void gatt_client_event_handler(
275273
*/
276274
case ESP_GATTC_READ_CHAR_EVT: {
277275
if (param->read.status == ESP_GATT_OK) {
278-
BLEDevice *pDevice = BLEUtils::findByConnId(param->read.conn_id);
276+
BLERemoteDevice *pDevice = BLEUtils::findByConnId(param->read.conn_id);
279277
std::string data = std::string((char *)param->read.value, param->read.value_len);
280278
pDevice->onRead(data);
281279
}
@@ -299,10 +297,9 @@ static void gap_event_handler(
299297
BLEUtils::dumpGapEvent(event, param);
300298

301299
switch(event) {
300+
/*
302301
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());
302+
BLERemoteDevice device;
306303
307304
if (param->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT) {
308305
//ESP_LOGD(tag, "num_resps: %d", param->scan_rst.num_resps);
@@ -315,23 +312,24 @@ static void gap_event_handler(
315312
//ESP_LOGD(tag, "rssi: %d", param->scan_rst.rssi);
316313
//ESP_LOGD(tag, "addr_type: %s", bt_addr_t_to_string(param->scan_rst.ble_addr_type));
317314
//ESP_LOGD(tag, "flag: %d", param->scan_rst.flag);
318-
device.setAddress(std::string((char *)param->scan_rst.bda, 6));
315+
device.setAddress(BLEAddress(param->scan_rst.bda));
319316
device.setRSSI(param->scan_rst.rssi);
320317
device.setAdFlag(param->scan_rst.flag);
321318
//device.dump();
322319
device.parsePayload((uint8_t *)param->scan_rst.ble_adv);
323-
g_devices.insert(std::pair<std::string,BLEDevice>(device.getAddress(),device));
320+
g_devices.insert(std::pair<std::string,BLERemoteDevice>(device.getAddress().toString(),device));
324321
//dump_adv_payload(param->scan_rst.ble_adv);
325322
} else {
326-
ESP_LOGD(LOG_TAG, "Unhandled search_evt type!");
323+
ESP_LOGD(LOG_TAG, "Un-handled search_evt type!");
327324
}
328325
break;
329326
} // ESP_GAP_BLE_SCAN_RESULT_EVT
327+
*/
330328

331329
case ESP_GAP_BLE_SEC_REQ_EVT: {
332330
esp_err_t errRc = ::esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true);
333331
if (errRc != ESP_OK) {
334-
ESP_LOGE(LOG_TAG, "esp_ble_gap_security_rsp: rc=%d %s", errRc, espToString(errRc));
332+
ESP_LOGE(LOG_TAG, "esp_ble_gap_security_rsp: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
335333
}
336334
break;
337335
}
@@ -352,7 +350,7 @@ static void gap_event_handler(
352350
/**
353351
* @brief Get the current set of known devices.
354352
*/
355-
std::map<ble_address, BLEDevice> getDevices() {
353+
std::map<std::string, BLERemoteDevice> getDevices() {
356354
return g_devices;
357355
} // getDevices
358356

@@ -365,51 +363,51 @@ std::map<ble_address, BLEDevice> getDevices() {
365363
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
366364
esp_err_t errRc = esp_bt_controller_init(&bt_cfg);
367365
if (errRc != ESP_OK) {
368-
ESP_LOGE(LOG_TAG, "esp_bt_controller_init: rc=%d %s", errRc, espToString(errRc));
366+
ESP_LOGE(LOG_TAG, "esp_bt_controller_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
369367
return;
370368
}
371369

372370

373371
errRc = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
374372
if (errRc != ESP_OK) {
375-
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, espToString(errRc));
373+
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
376374
return;
377375
}
378376

379377
errRc = esp_bluedroid_init();
380378
if (errRc != ESP_OK) {
381-
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, espToString(errRc));
379+
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
382380
return;
383381
}
384382

385383
errRc = esp_bluedroid_enable();
386384
if (errRc != ESP_OK) {
387-
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, espToString(errRc));
385+
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
388386
return;
389387
}
390388

391389
errRc = esp_ble_gap_register_callback(gap_event_handler);
392390
if (errRc != ESP_OK) {
393-
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, espToString(errRc));
391+
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
394392
return;
395393
}
396394

397395
errRc = esp_ble_gatts_register_callback(gatt_server_event_handler);
398396
if (errRc != ESP_OK) {
399-
ESP_LOGE(LOG_TAG, "esp_ble_gatts_register_callback: rc=%d %s", errRc, espToString(errRc));
397+
ESP_LOGE(LOG_TAG, "esp_ble_gatts_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
400398
return;
401399
}
402400

403401
errRc = ::esp_ble_gap_set_device_name(deviceName.c_str());
404402
if (errRc != ESP_OK) {
405-
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_device_name: rc=%d %s", errRc, espToString(errRc));
403+
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_device_name: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
406404
return;
407405
};
408406

409407
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
410408
errRc = ::esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
411409
if (errRc != ESP_OK) {
412-
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_security_param: rc=%d %s", errRc, espToString(errRc));
410+
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_security_param: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
413411
return;
414412
};
415413

@@ -424,44 +422,44 @@ void BLE::initClient() {
424422
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
425423
esp_err_t errRc = esp_bt_controller_init(&bt_cfg);
426424
if (errRc != ESP_OK) {
427-
ESP_LOGE(LOG_TAG, "esp_bt_controller_init: rc=%d %s", errRc, espToString(errRc));
425+
ESP_LOGE(LOG_TAG, "esp_bt_controller_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
428426
return;
429427
}
430428

431429

432430
errRc = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
433431
if (errRc != ESP_OK) {
434-
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, espToString(errRc));
432+
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
435433
return;
436434
}
437435

438436
errRc = esp_bluedroid_init();
439437
if (errRc != ESP_OK) {
440-
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, espToString(errRc));
438+
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
441439
return;
442440
}
443441

444442
errRc = esp_bluedroid_enable();
445443
if (errRc != ESP_OK) {
446-
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, espToString(errRc));
444+
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
447445
return;
448446
}
449447

450448
errRc = esp_ble_gap_register_callback(gap_event_handler);
451449
if (errRc != ESP_OK) {
452-
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, espToString(errRc));
450+
ESP_LOGE(LOG_TAG, "esp_ble_gap_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
453451
return;
454452
}
455453

456454
errRc = esp_ble_gattc_register_callback(gatt_client_event_handler);
457455
if (errRc != ESP_OK) {
458-
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_callback: rc=%d %s", errRc, espToString(errRc));
456+
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
459457
return;
460458
}
461459

462460
errRc = esp_ble_gattc_app_register(0);
463461
if (errRc != ESP_OK) {
464-
ESP_LOGE(LOG_TAG, "esp_ble_gattc_app_register: rc=%d %s", errRc, espToString(errRc));
462+
ESP_LOGE(LOG_TAG, "esp_ble_gattc_app_register: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
465463
return;
466464
}
467465
g_eventGroup = xEventGroupCreate();
@@ -479,6 +477,7 @@ void BLE::initClient() {
479477
* @param [in] scanType The type of scanning requested. The choices are `BLE_SCA_TYPE_PASSIVE` and `BLE_SCAN_TYPE_ACTIVE`.
480478
* The distinction between them is whether or not the advertizer has a scan response requested from it.
481479
*/
480+
/*
482481
void BLE::scan(int duration, esp_ble_scan_type_t scan_type) {
483482
g_devices.clear();
484483
static esp_ble_scan_params_t ble_scan_params;
@@ -489,7 +488,7 @@ void BLE::scan(int duration, esp_ble_scan_type_t scan_type) {
489488
ble_scan_params.scan_window = 0x30;
490489
esp_err_t errRc = esp_ble_gap_set_scan_params(&ble_scan_params);
491490
if (errRc != ESP_OK) {
492-
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_scan_params: rc=%d %s", errRc, espToString(errRc));
491+
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_scan_params: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
493492
return;
494493
}
495494
@@ -498,15 +497,13 @@ void BLE::scan(int duration, esp_ble_scan_type_t scan_type) {
498497
ESP_LOGE(LOG_TAG, "esp_ble_gap_start_scanning: rc=%d", errRc);
499498
return;
500499
}
501-
/*
502-
* Block until done
503-
*/
500+
504501
xEventGroupWaitBits(g_eventGroup,
505502
EVENT_GROUP_SCAN_COMPLETE,
506503
1, // Clear on exit
507504
0, // Wait for all bits
508505
portMAX_DELAY);
509506
ESP_LOGD(LOG_TAG, "Scan complete! - BLE:scan() returning.");
510507
} // scan
511-
508+
*/
512509
#endif // CONFIG_BT_ENABLED

cpp_utils/BLE.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <string>
1616

1717
#include "BLEServer.h"
18-
#include "BLEDevice.h"
18+
#include "BLERemoteDevice.h"
1919
#include "BLEUtils.h"
2020
/**
2121
* @brief %BLE functions.
@@ -25,11 +25,11 @@ class BLE {
2525
BLE();
2626
virtual ~BLE();
2727
static void dumpDevices();
28-
static std::map<ble_address, BLEDevice> getDevices();
28+
static std::map<std::string, BLERemoteDevice> getDevices();
2929

3030
static void initClient();
3131
static void initServer(std::string deviceName);
32-
static void scan(int duration, esp_ble_scan_type_t scan_type = BLE_SCAN_TYPE_PASSIVE);
32+
//static void scan(int duration, esp_ble_scan_type_t scan_type = BLE_SCAN_TYPE_PASSIVE);
3333
static esp_gatt_if_t getGattcIF();
3434
static BLEServer *m_bleServer;
3535
}; // class BLE

cpp_utils/BLEAddress.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* BLEAddress.cpp
3+
*
4+
* Created on: Jul 2, 2017
5+
* Author: kolban
6+
*/
7+
8+
#include "BLEAddress.h"
9+
#include <string>
10+
#include <sstream>
11+
#include <iomanip>
12+
#include <string.h>
13+
#include <stdio.h>
14+
15+
BLEAddress::BLEAddress(esp_bd_addr_t address) {
16+
memcpy(m_address, address, ESP_BD_ADDR_LEN);
17+
}
18+
19+
BLEAddress::~BLEAddress() {
20+
}
21+
22+
BLEAddress::BLEAddress(std::string stringAddress) {
23+
if (stringAddress.length() != 17) {
24+
return;
25+
}
26+
int data[6];
27+
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5]);
28+
m_address[0] = (uint8_t)data[0];
29+
m_address[1] = (uint8_t)data[1];
30+
m_address[2] = (uint8_t)data[2];
31+
m_address[3] = (uint8_t)data[3];
32+
m_address[4] = (uint8_t)data[4];
33+
m_address[5] = (uint8_t)data[5];
34+
}
35+
36+
/**
37+
* @brief Convert a BLE address to a string.
38+
*
39+
* @return The string representation of the address.
40+
*/
41+
std::string BLEAddress::toString() {
42+
std::stringstream stream;
43+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[0] << ':';
44+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[1] << ':';
45+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[2] << ':';
46+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[3] << ':';
47+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[4] << ':';
48+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[5];
49+
return stream.str();
50+
} // toString
51+
52+
esp_bd_addr_t *BLEAddress::getNative() {
53+
return &m_address;
54+
}

cpp_utils/BLEAddress.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* BLEAddress.h
3+
*
4+
* Created on: Jul 2, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef COMPONENTS_CPP_UTILS_BLEADDRESS_H_
9+
#define COMPONENTS_CPP_UTILS_BLEADDRESS_H_
10+
#include <esp_gap_ble_api.h> // ESP32 BLE
11+
#include <string>
12+
13+
class BLEAddress {
14+
public:
15+
BLEAddress(esp_bd_addr_t address);
16+
BLEAddress(std::string stringAddress);
17+
virtual ~BLEAddress();
18+
std::string toString();
19+
esp_bd_addr_t *getNative();
20+
private:
21+
esp_bd_addr_t m_address;
22+
};
23+
24+
#endif /* COMPONENTS_CPP_UTILS_BLEADDRESS_H_ */

0 commit comments

Comments
 (0)
0