blescan.cpp optimization

This commit is contained in:
Klaus K Wilting 2018-04-15 00:51:28 +02:00
parent 666d2e0e32
commit 4f8bb26fc0
2 changed files with 108 additions and 93 deletions

View File

@ -31,6 +31,7 @@ build_flags =
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
;override lora settings from LMiC library in lmic/config.h and use main.h instead
-D_lmic_config_h_
-Dheltec_wifi_lora_32
@ -135,7 +136,7 @@ framework = arduino
monitor_baud = 115200
upload_speed = 256000
lib_deps =
U8g2
U8g2@>2.21.7
SmartLeds
build_flags =
;set log level, we need build_flag for this, otherwise we can't use ESP_LOGx in arduino framework
@ -157,7 +158,7 @@ framework = arduino
monitor_baud = 115200
upload_speed = 921600
lib_deps =
U8g2
U8g2@>2.21.7
SmartLeds
build_flags =
;set log level, we need build_flag for this, otherwise we can't use ESP_LOGx in arduino framework

View File

@ -16,6 +16,9 @@ https://github.com/nkolban/esp32-snippets/tree/master/BLE/scanner
#define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
// local Tag for logging
static const char *TAG = "bt_loop";
// defined in macsniff.cpp
bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type);
@ -24,24 +27,20 @@ static const char *bt_event_type_to_string(uint32_t eventType);
static const char *bt_gap_search_event_type_to_string(uint32_t searchEvt);
static const char *bt_addr_t_to_string(esp_ble_addr_type_t type);
static const char *bt_dev_type_to_string(esp_bt_dev_type_t type);
static void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
static const char *btsig_gap_type(uint32_t gap_type);
// local Tag for logging
static const char *TAG = "bt_loop";
static void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
ESP_LOGD(TAG, "Received a GAP event: %s", bt_event_type_to_string(event));
esp_ble_gap_cb_param_t *p = (esp_ble_gap_cb_param_t *)param;
esp_err_t status;
switch (event)
{
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT:
{
ESP_LOGD(TAG, "status: %d", p->scan_param_cmpl.status);
ESP_LOGD(TAG, "Start Scan, status: %d", p->scan_param_cmpl.status);
// This procedure keep the device scanning the peer device which advertising on the air.
status = esp_ble_gap_start_scanning(BLESCANTIME);
@ -52,16 +51,6 @@ static void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_pa
}
break;
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:
{
//scan start complete event to indicate scan start successfully or failed
if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS)
{
ESP_LOGE(TAG, "Scan start failed");
}
}
break;
case ESP_GAP_BLE_SCAN_RESULT_EVT:
{
@ -80,8 +69,46 @@ static void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_pa
ESP_LOGI(TAG, "num_resps : %d", p->scan_rst.num_resps);
/* to be done here:
if ( p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT) // Inquiry complete, scan is done
{ // restart scan
status = esp_ble_gap_start_scanning (BLESCANTIME);
if (status != ESP_OK)
{
ESP_LOGE(TAG, "esp_ble_gap_start_scanning: rc=%d", status);
}
return;
}
if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) // Inquiry result for a peer device
{
#ifdef VENDORFILTER
if ( (p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_PUBLIC) ||
(p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RPA_PUBLIC)
)
{
#endif
// add this device and show new count total if it was not previously added
mac_add((uint8_t *) p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE);
#ifdef VENDORFILTER
}
else
{
ESP_LOGI(TAG, "BLE device filtered");
}
#endif
/* to be improved in vendorfilter if:
// you can search for elements in the payload using the
// function esp_ble_resolve_adv_data()
//
// Like this, that scans for the "Complete name" (looking inside the payload buffer)
// uint8_t len;
// uint8_t *data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv, ESP_BLE_AD_TYPE_NAME_CMPL, &len);
filter BLE devices using their advertisements to get filter alternative to vendor OUI
if vendorfiltering is on, we ...
@ -100,71 +127,13 @@ static void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_pa
device (smartphone, handsfree, computer, LAN/network AP). With this parameter we can
differentiate among pedestrians and vehicles."
#endif
*/
// add this device and show new count total if it was not previously added
mac_add((uint8_t *) p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE);
if ( p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT)
{
// Scan is done.
// The next 5 codelines automatically restarts the scan.
status = esp_ble_gap_start_scanning (BLESCANTIME);
if (status != ESP_OK)
{
ESP_LOGE(TAG, "esp_ble_gap_start_scanning: rc=%d", status);
}
return;
}
if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT)
{
// you can search for elements in the payload using the
// function esp_ble_resolve_adv_data()
//
// Like this, that scans for the "Complete name" (looking inside the payload buffer)
uint8_t len;
uint8_t *data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv, ESP_BLE_AD_TYPE_NAME_CMPL, &len);
ESP_LOGD(TAG, "len: %d, %.*s", len, len, data);
}
ESP_LOGI(TAG, "");
}
break;
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
{
if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS)
{
ESP_LOGE(TAG, "Scan stop failed");
}
else
{
ESP_LOGI(TAG, "Stop scan successfully");
}
}
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
{
if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS)
{
ESP_LOGE(TAG, "Adv stop failed");
}
else
{
ESP_LOGI(TAG, "Stop adv successfully");
}
}
break;
default:
break;
}
@ -237,7 +206,51 @@ static const char *bt_event_type_to_string(uint32_t eventType) {
}
} // bt_event_type_to_string
static const char *btsig_gap_type(uint32_t gap_type) {
switch (gap_type)
{
case 0x01: return "Flags";
case 0x02: return "Incomplete List of 16-bit Service Class UUIDs";
case 0x03: return "Complete List of 16-bit Service Class UUIDs";
case 0x04: return "Incomplete List of 32-bit Service Class UUIDs";
case 0x05: return "Complete List of 32-bit Service Class UUIDs";
case 0x06: return "Incomplete List of 128-bit Service Class UUIDs";
case 0x07: return "Complete List of 128-bit Service Class UUIDs";
case 0x08: return "Shortened Local Name";
case 0x09: return "Complete Local Name";
case 0x0A: return "Tx Power Level";
case 0x0D: return "Class of Device";
case 0x0E: return "Simple Pairing Hash C/C-192";
case 0x0F: return "Simple Pairing Randomizer R/R-192";
case 0x10: return "Device ID/Security Manager TK Value";
case 0x11: return "Security Manager Out of Band Flags";
case 0x12: return "Slave Connection Interval Range";
case 0x14: return "List of 16-bit Service Solicitation UUIDs";
case 0x1F: return "List of 32-bit Service Solicitation UUIDs";
case 0x15: return "List of 128-bit Service Solicitation UUIDs";
case 0x16: return "Service Data - 16-bit UUID";
case 0x20: return "Service Data - 32-bit UUID";
case 0x21: return "Service Data - 128-bit UUID";
case 0x22: return "LE Secure Connections Confirmation Value";
case 0x23: return "LE Secure Connections Random Value";
case 0x24: return "URI";
case 0x25: return "Indoor Positioning";
case 0x26: return "Transport Discovery Data";
case 0x17: return "Public Target Address";
case 0x18: return "Random Target Address";
case 0x19: return "Appearance";
case 0x1A: return "Advertising Interval";
case 0x1B: return "LE Bluetooth Device Address";
case 0x1C: return "LE Role";
case 0x1D: return "Simple Pairing Hash C-256";
case 0x1E: return "Simple Pairing Randomizer R-256";
case 0x3D: return "3D Information Data";
case 0xFF: return "Manufacturer Specific Data";
default:
return "Unknown type";
}
}
esp_err_t register_ble_functionality(void)
{
@ -258,7 +271,8 @@ esp_err_t register_ble_functionality(void)
{
.scan_type = BLE_SCAN_TYPE_PASSIVE,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_UND_RPA_DIR,
// .scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
.scan_interval = (uint16_t) (BLESCANINTERVAL / 0.625), // Time = N * 0.625 msec
.scan_window = (uint16_t) (BLESCANWINDOW / 0.625) // Time = N * 0.625 msec
};
@ -283,7 +297,7 @@ void bt_loop(void *ignore)
esp_err_t status;
// Initialize BT controller to allocate task and other resource.
ESP_LOGI(TAG, "Enabling Bluetooth Controller...");
ESP_LOGI(TAG, "Enabling Bluetooth Controller");
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
if (esp_bt_controller_init(&bt_cfg) != ESP_OK)
{
@ -299,7 +313,7 @@ void bt_loop(void *ignore)
}
// Init and alloc the resource for bluetooth, must be prior to every bluetooth stuff
ESP_LOGI(TAG, "Init Bluetooth stack...");
ESP_LOGI(TAG, "Init Bluetooth stack");
status = esp_bluedroid_init();
if (status != ESP_OK)
{
@ -315,7 +329,7 @@ void bt_loop(void *ignore)
goto end;
}
ESP_LOGI(TAG, "Register BLE functionality...");
ESP_LOGI(TAG, "Register BLE functionality");
status = register_ble_functionality();
if (status != ESP_OK)
{