From f338bf6fae1210f243703ec6a00108b68ff53d2a Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Tue, 16 Jul 2024 15:39:51 +0200 Subject: [PATCH] Bluetooth: Host: Discard advertising data if not explicit scanning If the application is not explicitly scanning, then there is not really any need to parse advertising reports nor send them to the application. Signed-off-by: Emil Gydesen --- subsys/bluetooth/host/scan.c | 27 +++++++++++++++++++ tests/bluetooth/host_long_adv_recv/src/main.c | 11 +++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/subsys/bluetooth/host/scan.c b/subsys/bluetooth/host/scan.c index 5cf6a00719b..c64a1bb9e57 100644 --- a/subsys/bluetooth/host/scan.c +++ b/subsys/bluetooth/host/scan.c @@ -6,6 +6,7 @@ */ #include +#include #include #include @@ -609,6 +610,21 @@ void bt_hci_le_adv_ext_report(struct net_buf *buf) bool more_to_come; bool is_new_advertiser; + if (!atomic_test_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) { + /* The application has not requested explicit scan, so it is not expecting + * advertising reports. Discard, and reset the reassembler if not inactive + * This is done in the loop as this flag can change between each iteration, + * and it is not uncommon that scanning is disabled in the callback called + * from le_adv_recv + */ + + if (reassembling_advertiser.state != FRAG_ADV_INACTIVE) { + reset_reassembling_advertiser(); + } + + break; + } + if (buf->len < sizeof(*evt)) { LOG_ERR("Unexpected end of buffer"); break; @@ -1457,6 +1473,17 @@ void bt_hci_le_adv_report(struct net_buf *buf) while (num_reports--) { struct bt_le_scan_recv_info adv_info; + if (!atomic_test_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) { + /* The application has not requested explicit scan, so it is not expecting + * advertising reports. Discard. + * This is done in the loop as this flag can change between each iteration, + * and it is not uncommon that scanning is disabled in the callback called + * from le_adv_recv + */ + + break; + } + if (buf->len < sizeof(*evt)) { LOG_ERR("Unexpected end of buffer"); break; diff --git a/tests/bluetooth/host_long_adv_recv/src/main.c b/tests/bluetooth/host_long_adv_recv/src/main.c index 4fb36499055..71769be589d 100644 --- a/tests/bluetooth/host_long_adv_recv/src/main.c +++ b/tests/bluetooth/host_long_adv_recv/src/main.c @@ -1,11 +1,14 @@ /* main.c - Host long advertising receive */ /* - * Copyright (c) 2021 Nordic Semiconductor ASA + * Copyright (c) 2021-2024 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ +#include + +#include #include #include #include @@ -18,6 +21,7 @@ #include #include #include +#include #define DT_DRV_COMPAT zephyr_bt_hci_test @@ -201,6 +205,7 @@ static void le_read_supp_states(struct net_buf *buf, struct net_buf **evt, uint8 (void)memset(&rp->le_states, 0xFF, sizeof(rp->le_states)); } + /* Setup handlers needed for bt_enable to function. */ static const struct cmd_handler cmds[] = { { BT_HCI_OP_READ_LOCAL_VERSION_INFO, sizeof(struct bt_hci_rp_read_local_version_info), @@ -219,6 +224,8 @@ static const struct cmd_handler cmds[] = { { BT_HCI_OP_LE_RAND, sizeof(struct bt_hci_rp_le_rand), generic_success }, { BT_HCI_OP_LE_SET_RANDOM_ADDRESS, sizeof(struct bt_hci_cp_le_set_random_address), generic_success }, + { BT_HCI_OP_LE_SET_EXT_SCAN_PARAM, 0, generic_success }, + { BT_HCI_OP_LE_SET_EXT_SCAN_ENABLE, 0, generic_success }, { BT_HCI_OP_RESET, 0, generic_success }, }; @@ -382,6 +389,8 @@ ZTEST(long_adv_rx_tests, test_host_long_adv_recv) static struct bt_le_scan_cb scan_callbacks = { .recv = scan_recv_cb, .timeout = scan_timeout_cb }; bt_le_scan_cb_register(&scan_callbacks); + zassert_true((bt_le_scan_start(BT_LE_SCAN_PASSIVE, NULL) == 0), "bt_le_scan_start failed"); + bt_addr_le_t addr_a; bt_addr_le_t addr_b; bt_addr_le_t addr_c;