Skip to content

Commit

Permalink
Merge branch 'feature/update_ble_duplicte_vs_hci_lib_fix' into 'master'
Browse files Browse the repository at this point in the history
Feature/update ble duplicte vs hci lib fix

See merge request espressif/esp-idf!24521
  • Loading branch information
Isl2017 committed Jul 5, 2023
2 parents 1cd3ace + d9d9d4c commit a7e5d49
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 4 deletions.
71 changes: 71 additions & 0 deletions components/bt/controller/esp32c6/Kconfig.in
Expand Up @@ -422,3 +422,74 @@ 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.

config BT_LE_SCAN_DUPL_CACHE_REFRESH_PERIOD
int "Duplicate scan list refresh period (seconds)"
depends on BT_LE_SCAN_DUPL
range 0 1000
default 0
help
If the period value is non-zero, the controller will periodically clear the device information
stored in the scan duuplicate filter. If it is 0, the scan duuplicate filter will not be cleared
until the scanning is disabled. Duplicate advertisements for this period should not be sent to the
Host in advertising report events.
There are two scenarios where the ADV packet will be repeatedly reported:
1. The duplicate scan cache is full, the controller will delete the oldest device information and
add new device information.
2. When the refresh period is up, the controller will clear all device information and start filtering
again.
70 changes: 70 additions & 0 deletions components/bt/controller/esp32c6/bt.c
Expand Up @@ -617,6 +617,74 @@ 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);
extern void scan_duplicate_cache_refresh_set_time(uint32_t period_time);

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;
}

int ble_vhci_disc_duplicate_set_period_refresh_time(int refresh_period_time){
// TODO: use vendor hci to update
scan_duplicate_cache_refresh_set_time(refresh_period_time);
return true;
}

/**
* @brief Config scan duplicate option mode from menuconfig (Adapt to the old configuration method.)
*/
void ble_controller_scan_duplicate_config(void)
{
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 | FILTER_DUPLICATE_PDUTYPE;
} 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(0xFFFFFFFF);
ble_vhci_disc_duplicate_mode_enable(duplicate_mode);
ble_vhci_disc_duplicate_set_max_cache_size(cache_size);
ble_vhci_disc_duplicate_set_period_refresh_time(CONFIG_BT_LE_SCAN_DUPL_CACHE_REFRESH_PERIOD);
}

esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
{
uint8_t mac[6];
Expand Down Expand Up @@ -728,6 +796,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);
Expand Down
72 changes: 72 additions & 0 deletions components/bt/controller/esp32h2/Kconfig.in
Expand Up @@ -402,3 +402,75 @@ 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.

config BT_LE_SCAN_DUPL_CACHE_REFRESH_PERIOD
int "Duplicate scan list refresh period (seconds)"
depends on BT_LE_SCAN_DUPL
range 0 1000
default 0
help
If the period value is non-zero, the controller will periodically clear the device information
stored in the scan duuplicate filter. If it is 0, the scan duuplicate filter will not be cleared
until the scanning is disabled. Duplicate advertisements for this period should not be sent to the
Host in advertising report events.
There are two scenarios where the ADV packet will be repeatedly reported:
1. The duplicate scan cache is full, the controller will delete the oldest device information and
add new device information.
2. When the refresh period is up, the controller will clear all device information and start filtering
again.
70 changes: 70 additions & 0 deletions components/bt/controller/esp32h2/bt.c
Expand Up @@ -583,6 +583,74 @@ 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);
extern void scan_duplicate_cache_refresh_set_time(uint32_t period_time);

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;
}

int ble_vhci_disc_duplicate_set_period_refresh_time(int refresh_period_time){
// TODO: use vendor hci to update
scan_duplicate_cache_refresh_set_time(refresh_period_time);
return true;
}

/**
* @brief Config scan duplicate option mode from menuconfig (Adapt to the old configuration method.)
*/
void ble_controller_scan_duplicate_config(void)
{
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 | FILTER_DUPLICATE_PDUTYPE;
} 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(0xFFFFFFFF);
ble_vhci_disc_duplicate_mode_enable(duplicate_mode);
ble_vhci_disc_duplicate_set_max_cache_size(cache_size);
ble_vhci_disc_duplicate_set_period_refresh_time(CONFIG_BT_LE_SCAN_DUPL_CACHE_REFRESH_PERIOD);
}

esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
{
uint8_t mac[6];
Expand Down Expand Up @@ -668,6 +736,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);
Expand Down
2 changes: 1 addition & 1 deletion components/bt/controller/lib_esp32c6/esp32c6-bt-lib
2 changes: 1 addition & 1 deletion components/bt/controller/lib_esp32h2/esp32h2-bt-lib
2 changes: 1 addition & 1 deletion components/bt/include/esp32c6/include/esp_bt.h
Expand Up @@ -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, \
Expand Down
2 changes: 1 addition & 1 deletion components/bt/include/esp32h2/include/esp_bt.h
Expand Up @@ -258,7 +258,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, \
.cpu_freq_mhz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ, \
.ignore_wl_for_direct_adv = 0, \
Expand Down

0 comments on commit a7e5d49

Please sign in to comment.