From 6dc7d9b63f1769f07aa3b08644cb8a97538362bc Mon Sep 17 00:00:00 2001 From: Geng Yuchao Date: Thu, 15 Jun 2023 17:12:22 +0800 Subject: [PATCH] feat(ble): Add duplicate filter feature enable on ble for c6. --- components/bt/controller/esp32c6/Kconfig.in | 56 +++++++++++++++++ components/bt/controller/esp32c6/bt.c | 62 +++++++++++++++++++ .../bt/include/esp32c6/include/esp_bt.h | 2 +- 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/components/bt/controller/esp32c6/Kconfig.in b/components/bt/controller/esp32c6/Kconfig.in index db991124dd..9f77ea8520 100644 --- a/components/bt/controller/esp32c6/Kconfig.in +++ b/components/bt/controller/esp32c6/Kconfig.in @@ -422,3 +422,59 @@ config BT_LE_USE_ESP_TIMER help Set this option to use Esp Timer which has higher priority timer instead of FreeRTOS timer + +config BT_LE_SCAN_DUPL + bool "BLE Scan Duplicate Options" + default y + help + This select enables parameters setting of BLE scan duplicate. + +choice BT_LE_SCAN_DUPL_TYPE + prompt "Scan Duplicate Type" + default BT_LE_SCAN_DUPL_TYPE_DEVICE + depends on BT_LE_SCAN_DUPL + help + Scan duplicate have three ways. one is "Scan Duplicate By Device Address", This way is to use + advertiser address filtering. The adv packet of the same address is only allowed to be reported once. + Another way is "Scan Duplicate By Device Address And Advertising Data". This way is to use advertising + data and device address filtering. All different adv packets with the same address are allowed to be + reported. The last way is "Scan Duplicate By Advertising Data". This way is to use advertising data + filtering. All same advertising data only allow to be reported once even though they are from + different devices. + + config BT_LE_SCAN_DUPL_TYPE_DEVICE + bool "Scan Duplicate By Device Address" + help + This way is to use advertiser address filtering. The adv packet of the same address is only + allowed to be reported once + + config BT_LE_SCAN_DUPL_TYPE_DATA + bool "Scan Duplicate By Advertising Data" + help + This way is to use advertising data filtering. All same advertising data only allow to be reported + once even though they are from different devices. + + config BT_LE_SCAN_DUPL_TYPE_DATA_DEVICE + bool "Scan Duplicate By Device Address And Advertising Data" + help + This way is to use advertising data and device address filtering. All different adv packets with + the same address are allowed to be reported. +endchoice + +config BT_LE_SCAN_DUPL_TYPE + int + depends on BT_LE_SCAN_DUPL + default 0 if BT_LE_SCAN_DUPL_TYPE_DEVICE + default 1 if BT_LE_SCAN_DUPL_TYPE_DATA + default 2 if BT_LE_SCAN_DUPL_TYPE_DATA_DEVICE + default 0 + + +config BT_LE_SCAN_DUPL_CACHE_SIZE + int "Maximum number of devices in scan duplicate filter" + depends on BT_LE_SCAN_DUPL + range 10 1000 + default 100 + help + Maximum number of devices which can be recorded in scan duplicate filter. + When the maximum amount of device in the filter is reached, the cache will be refreshed. diff --git a/components/bt/controller/esp32c6/bt.c b/components/bt/controller/esp32c6/bt.c index 4cd73dbc78..f903b3497d 100644 --- a/components/bt/controller/esp32c6/bt.c +++ b/components/bt/controller/esp32c6/bt.c @@ -617,6 +617,66 @@ void controller_sleep_deinit(void) #endif //CONFIG_PM_ENABLE } +typedef enum { + FILTER_DUPLICATE_PDUTYPE = BIT(0), + FILTER_DUPLICATE_LENGTH = BIT(1), + FILTER_DUPLICATE_ADDRESS = BIT(2), + FILTER_DUPLICATE_ADVDATA = BIT(3), + FILTER_DUPLICATE_DEFAULT = FILTER_DUPLICATE_PDUTYPE | FILTER_DUPLICATE_ADDRESS, + FILTER_DUPLICATE_PDU_ALL = 0xF, + FILTER_DUPLICATE_EXCEPTION_FOR_MESH = BIT(4), + FILTER_DUPLICATE_AD_TYPE = BIT(5), +}disc_duplicate_mode_t; + + +extern void filter_duplicate_mode_enable(disc_duplicate_mode_t mode); +extern void filter_duplicate_mode_disable(disc_duplicate_mode_t mode); +extern void filter_duplicate_set_ring_list_max_num(uint32_t max_num); + +int +ble_vhci_disc_duplicate_mode_enable(int mode) +{ + // TODO: use vendor hci to update + filter_duplicate_mode_enable(mode); + return true; +} + +int +ble_vhci_disc_duplicate_mode_disable(int mode) +{ + // TODO: use vendor hci to update + filter_duplicate_mode_disable(mode); + return true; +} + +int ble_vhci_disc_duplicate_set_max_cache_size(int max_cache_size){ + // TODO: use vendor hci to update + filter_duplicate_set_ring_list_max_num(max_cache_size); + return true; +} + +/** + * @brief Config scan duplicate option mode from menuconfig (Adapt to the old configuration method.) + */ +void ble_controller_scan_duplicate_config() +{ + uint32_t duplicate_mode = FILTER_DUPLICATE_DEFAULT; + uint32_t cache_size = CONFIG_BT_LE_SCAN_DUPL_CACHE_SIZE; + if (CONFIG_BT_LE_SCAN_DUPL_TYPE == 0) { + duplicate_mode = FILTER_DUPLICATE_ADDRESS; + } else if (CONFIG_BT_LE_SCAN_DUPL_TYPE == 1) { + duplicate_mode = FILTER_DUPLICATE_ADVDATA; + } else if (CONFIG_BT_LE_SCAN_DUPL_TYPE == 2) { + duplicate_mode = FILTER_DUPLICATE_ADDRESS | FILTER_DUPLICATE_ADVDATA; + } + + duplicate_mode |= FILTER_DUPLICATE_EXCEPTION_FOR_MESH; + + ble_vhci_disc_duplicate_mode_disable(0xFF); + ble_vhci_disc_duplicate_mode_enable(duplicate_mode); + ble_vhci_disc_duplicate_set_max_cache_size(cache_size); +} + esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) { uint8_t mac[6]; @@ -730,6 +790,8 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) goto free_controller; } + ble_controller_scan_duplicate_config(); + ret = controller_sleep_init(); if (ret != ESP_OK) { ESP_LOGW(NIMBLE_PORT_LOG_TAG, "controller_sleep_init failed %d", ret); diff --git a/components/bt/include/esp32c6/include/esp_bt.h b/components/bt/include/esp32c6/include/esp_bt.h index f8eb6a2af9..929d73c4bc 100644 --- a/components/bt/include/esp32c6/include/esp_bt.h +++ b/components/bt/include/esp32c6/include/esp_bt.h @@ -256,7 +256,7 @@ typedef struct { .sleep_en = NIMBLE_SLEEP_ENABLE, \ .coex_phy_coded_tx_rx_time_limit = DEFAULT_BT_LE_COEX_PHY_CODED_TX_RX_TLIM_EFF, \ .dis_scan_backoff = NIMBLE_DISABLE_SCAN_BACKOFF, \ - .ble_scan_classify_filter_enable = 0, \ + .ble_scan_classify_filter_enable = 1, \ .main_xtal_freq = CONFIG_XTAL_FREQ, \ .version_num = efuse_hal_chip_revision(), \ .cpu_freq_mhz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ, \