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

Skip to content

Commit 340a505

Browse files
committed
2017-07-05 - sync
1 parent e311f0a commit 340a505

18 files changed

+732
-673
lines changed

cpp_utils/BLE.cpp

Lines changed: 11 additions & 171 deletions
A3E2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ static EventGroupHandle_t g_eventGroup;
4242
*/
4343
static std::map<std::string, BLERemoteDevice> g_devices;
4444

45-
BLEServer *BLE::m_bleServer;
45+
BLEServer *BLE::m_bleServer = nullptr;
46+
BLEScan *BLE::m_pScan = nullptr;
4647

4748
BLE::BLE() {
4849
}
@@ -66,107 +67,6 @@ void BLE::dumpDevices() {
6667
} // dumpDevices
6768

6869

69-
/**
70-
* https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile
71-
*/
72-
/*
73-
static const char *adv_type_to_string(uint8_t advType) {
74-
switch(advType) {
75-
case ESP_BLE_AD_TYPE_FLAG:
76-
return "ESP_BLE_AD_TYPE_FLAG";
77-
case ESP_BLE_AD_TYPE_16SRV_PART:
78-
return "ESP_BLE_AD_TYPE_16SRV_PART";
79-
case ESP_BLE_AD_TYPE_16SRV_CMPL:
80-
return "ESP_BLE_AD_TYPE_16SRV_CMPL";
81-
case ESP_BLE_AD_TYPE_32SRV_PART:
82-
return "ESP_BLE_AD_TYPE_32SRV_PART";
83-
case ESP_BLE_AD_TYPE_32SRV_CMPL:
84-
return "ESP_BLE_AD_TYPE_32SRV_CMPL";
85-
case ESP_BLE_AD_TYPE_128SRV_PART:
86-
return "ESP_BLE_AD_TYPE_128SRV_PART";
87-
case ESP_BLE_AD_TYPE_128SRV_CMPL:
88-
return "ESP_BLE_AD_TYPE_128SRV_CMPL";
89-
case ESP_BLE_AD_TYPE_NAME_SHORT:
90-
return "ESP_BLE_AD_TYPE_NAME_SHORT";
91-
case ESP_BLE_AD_TYPE_NAME_CMPL:
92-
return "ESP_BLE_AD_TYPE_NAME_CMPL";
93-
case ESP_BLE_AD_TYPE_TX_PWR:
94-
return "ESP_BLE_AD_TYPE_TX_PWR";
95-
case ESP_BLE_AD_TYPE_DEV_CLASS:
96-
return "ESP_BLE_AD_TYPE_DEV_CLASS";
97-
case ESP_BLE_AD_TYPE_SM_TK:
98-
return "ESP_BLE_AD_TYPE_SM_TK";
99-
case ESP_BLE_AD_TYPE_SM_OOB_FLAG:
100-
return "ESP_BLE_AD_TYPE_SM_OOB_FLAG";
101-
case ESP_BLE_AD_TYPE_INT_RANGE:
102-
return "ESP_BLE_AD_TYPE_INT_RANGE";
103-
case ESP_BLE_AD_TYPE_SOL_SRV_UUID:
104-
return "ESP_BLE_AD_TYPE_SOL_SRV_UUID";
105-
case ESP_BLE_AD_TYPE_128SOL_SRV_UUID:
106-
return "ESP_BLE_AD_TYPE_128SOL_SRV_UUID";
107-
case ESP_BLE_AD_TYPE_SERVICE_DATA:
108-
return "ESP_BLE_AD_TYPE_SERVICE_DATA";
109-
case ESP_BLE_AD_TYPE_PUBLIC_TARGET:
110-
return "ESP_BLE_AD_TYPE_PUBLIC_TARGET";
111-
case ESP_BLE_AD_TYPE_RANDOM_TARGET:
112-
return "ESP_BLE_Amap1D_TYPE_RANDOM_TARGET";
113-
case ESP_BLE_AD_TYPE_APPEARANCE:
114-
return "ESP_BLE_AD_TYPE_APPEARANCE";
115-
case ESP_BLE_AD_TYPE_ADV_INT:
116-
return "ESP_BLE_AD_TYPE_ADV_INT";
117-
case ESP_BLE_AD_TYPE_32SOL_SRV_UUID:
118-
return "ESP_BLE_AD_TYPE_32SOL_SRV_UUID";
119-
case ESP_BLE_AD_TYPE_32SERVICE_DATA:
120-
return "ESP_BLE_AD_TYPE_32SERVICE_DATA";
121-
case ESP_BLE_AD_TYPE_128SERVICE_DATA:
122-
return "ESP_BLE_AD_TYPE_128SERVICE_DATA";
123-
case ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE:
124-
return "ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE";
125-
default:
126-
ESP_LOGD(tag, "Unknown adv data type: 0x%x", advType);
127-
return "Unknown";
128-
}
129-
}
130-
*/
131-
132-
/**
133-
* @brief Dump the advertizing payload.
134-
*
135-
* The payload is a buffer of bytes that is either 31 bytes long or terminated by
136-
* a 0 length value. Each entry in the buffer has the format:
137-
* [length][type][data...]
138-
*
139-
* The length does not include itself but does include everything after it until the next record. A record
140-
* with a length value of 0 indicates a terminator.
141-
*/
142-
/*
143-
static void dump_adv_payload(uint8_t *payload) {
144-
uint8_t length;
145-
uint8_t ad_type;
146-
uint8_t sizeConsumed = 0;
147-
bool finished = false;
148-
//int i;
149-
//char text[31*2+1];
150-
//sprintf(text, "%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x")
151-
while(!finished) {
152-
length = *payload;
153-
payload++;
154-
if (length != 0) {
155-
ad_type = *payload;
156-
payload += length;
157-
ESP_LOGD(tag, "Type: 0x%.2x (%s), length: %d", ad_type, adv_type_to_string(ad_type), length);
158-
159-
}
160-
161-
sizeConsumed = 1 + length;
162-
if (sizeConsumed >=31 || length == 0) {
163-
finished = true;
164-
}
165-
} // !finished
166-
} // dump_adv_payload
167-
*/
168-
169-
17070
/**
17171
* @brief Handle GATT server events.
17272
*
@@ -297,35 +197,6 @@ static void gap_event_handler(
297197
BLEUtils::dumpGapEvent(event, param);
298198

299199
switch(event) {
300-
/*
301-
case ESP_GAP_BLE_SCAN_RESULT_EVT: {
302-
BLERemoteDevice device;
303-
304-
if (param->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT) {
305-
//ESP_LOGD(tag, "num_resps: %d", param->scan_rst.num_resps);
306-
//BLE::dumpDevices();
307-
xEventGroupSetBits(g_eventGroup, EVENT_GROUP_SCAN_COMPLETE);
308-
}
309-
else if (param->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) {
310-
//ESP_LOGD(tag, "device type: %s", bt_dev_type_to_string(param->scan_rst.dev_type));
311-
//ESP_LOGD(tag, "device address (bda): %02x:%02x:%02x:%02x:%02x:%02x", BT_BD_ADDR_HEX(param->scan_rst.bda));
312-
//ESP_LOGD(tag, "rssi: %d", param->scan_rst.rssi);
313-
//ESP_LOGD(tag, "addr_type: %s", bt_addr_t_to_string(param->scan_rst.ble_addr_type));
314-
//ESP_LOGD(tag, "flag: %d", param->scan_rst.flag);
315-
device.setAddress(BLEAddress(param->scan_rst.bda));
316-
device.setRSSI(param->scan_rst.rssi);
317-
device.setAdFlag(param->scan_rst.flag);
318-
//device.dump();
319-
device.parsePayload((uint8_t *)param->scan_rst.ble_adv);
320-
g_devices.insert(std::pair<std::string,BLERemoteDevice>(device.getAddress().toString(),device));
321-
//dump_adv_payload(param->scan_rst.ble_adv);
322-
} else {
323-
ESP_LOGD(LOG_TAG, "Un-handled search_evt type!");
324-
}
325-
break;
326-
} // ESP_GAP_BLE_SCAN_RESULT_EVT
327-
*/
328-
329200
case ESP_GAP_BLE_SEC_REQ_EVT: {
330201
esp_err_t errRc = ::esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true);
331202
if (errRc != ESP_OK) {
@@ -339,11 +210,13 @@ static void gap_event_handler(
339210
}
340211
} // switch
341212

342-
343-
344213
if (BLE::m_bleServer != nullptr) {
345214
BLE::m_bleServer->handleGAPEvent(event, param);
346215
}
216+
217+
if (BLE::getScan() != nullptr) {
218+
BLE::getScan()->gapEventHandler(event, param);
219+
}
347220
} // gap_event_handler
348221

349222

@@ -466,44 +339,11 @@ void BLE::initClient() {
466339
xEventGroupClearBits(g_eventGroup, 0xff);
467340
} // init
468341

469-
470-
471-
/**
472-
* @brief Perform a %BLE scan.
473-
*
474-
* We scan for BLE devices that are advertizing.
475-
*
476-
* @param [in] duration The duration that the scan is to run for measured in seconds.
477-
* @param [in] scanType The type of scanning requested. The choices are `BLE_SCA_TYPE_PASSIVE` and `BLE_SCAN_TYPE_ACTIVE`.
478-
* The distinction between them is whether or not the advertizer has a scan response requested from it.
479-
*/
480-
/*
481-
void BLE::scan(int duration, esp_ble_scan_type_t scan_type) {
482-
g_devices.clear();
483-
static esp_ble_scan_params_t ble_scan_params;
484-
ble_scan_params.scan_type = scan_type;
485-
ble_scan_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
486-
ble_scan_params.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL;
487-
ble_scan_params.scan_interval = 0x50;
488-
ble_scan_params.scan_window = 0x30;
489-
esp_err_t errRc = esp_ble_gap_set_scan_params(&ble_scan_params);
490-
if (errRc != ESP_OK) {
491-
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_scan_params: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
492-
return;
493-
}
494-
495-
errRc = esp_ble_gap_start_scanning(duration);
496-
if (errRc != ESP_OK) {
497-
ESP_LOGE(LOG_TAG, "esp_ble_gap_start_scanning: rc=%d", errRc);
498-
return;
342+
BLEScan* BLE::getScan() {
343+
if (m_pScan == nullptr) {
344+
m_pScan = new BLEScan();
499345
}
346+
return m_pScan;
347+
} // getScan
500348

501-
xEventGroupWaitBits(g_eventGroup,
502-
EVENT_GROUP_SCAN_COMPLETE,
503-
1, // Clear on exit
504-
0, // Wait for all bits
505-
portMAX_DELAY);
506-
ESP_LOGD(LOG_TAG, "Scan complete! - BLE:scan() returning.");
507-
} // scan
508-
*/
509349
#endif // CONFIG_BT_ENABLED

cpp_utils/BLE.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "BLEServer.h"
1818
#include "BLERemoteDevice.h"
1919
#include "BLEUtils.h"
20+
#include "BLEScan.h"
2021
/**
2122
* @brief %BLE functions.
2223
*/
@@ -30,8 +31,10 @@ class BLE {
3031
static void initClient();
3132
static void initServer(std::string deviceName);
3233
//static void scan(int duration, esp_ble_scan_type_t scan_type = BLE_SCAN_TYPE_PASSIVE);
34+
static BLEScan *getScan();
3335
static esp_gatt_if_t getGattcIF();
3436
static BLEServer *m_bleServer;
37+
static BLEScan *m_pScan;
3538
}; // class BLE
3639

3740
#endif // CONFIG_BT_ENABLED

0 commit comments

Comments
 (0)
0