Skip to content

Commit

Permalink
pw_bluetooth_sapphire: Pass offloaded packet filter support down stack
Browse files Browse the repository at this point in the history
Bug: 42174576
Change-Id: I24822fd5608e6136426e5fee4f3b21cbc33847eb
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/260219
Commit-Queue: Faraaz Sareshwala <[email protected]>
Reviewed-by: Ben Lawson <[email protected]>
Docs-Not-Needed: Faraaz Sareshwala <[email protected]>
Lint: Lint 🤖 <[email protected]>
  • Loading branch information
Faraaz Sareshwala authored and CQ Bot Account committed Feb 10, 2025
1 parent a76d73e commit 5b8a5c5
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 43 deletions.
38 changes: 33 additions & 5 deletions pw_bluetooth_sapphire/host/gap/adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "pw_bluetooth_sapphire/internal/host/hci/legacy_low_energy_advertiser.h"
#include "pw_bluetooth_sapphire/internal/host/hci/legacy_low_energy_scanner.h"
#include "pw_bluetooth_sapphire/internal/host/hci/low_energy_connector.h"
#include "pw_bluetooth_sapphire/internal/host/hci/low_energy_scanner.h"
#include "pw_bluetooth_sapphire/internal/host/hci/sequential_command_runner.h"
#include "pw_bluetooth_sapphire/internal/host/l2cap/channel_manager.h"
#include "pw_bluetooth_sapphire/internal/host/sm/security_manager.h"
Expand Down Expand Up @@ -435,6 +436,28 @@ class AdapterImpl final : public Adapter {
void ParseLEGetVendorCapabilitiesCommandComplete(
const hci::EventPacket& event);

hci::LowEnergyScanner::PacketFilterConfig GetPacketFilterConfig() {
bool offloading_enabled = false;
uint8_t max_filters = 0;

constexpr pw::bluetooth::Controller::FeaturesBits feature =
pw::bluetooth::Controller::FeaturesBits::kAndroidVendorExtensions;
if (state().IsControllerFeatureSupported(feature) &&
state().android_vendor_capabilities &&
state().android_vendor_capabilities->supports_filtering()) {
bt_log(INFO,
"gap",
"controller supports android vendor extensions packet filtering, "
"max offloaded filters: %d",
max_filters);
offloading_enabled = true;
max_filters = state().android_vendor_capabilities->max_filters();
}

return hci::LowEnergyScanner::PacketFilterConfig(offloading_enabled,
max_filters);
}

std::unique_ptr<hci::LowEnergyAdvertiser> CreateAdvertiser(bool extended) {
if (extended) {
return std::make_unique<hci::ExtendedLowEnergyAdvertiser>(
Expand Down Expand Up @@ -477,14 +500,16 @@ class AdapterImpl final : public Adapter {
extended);
}

std::unique_ptr<hci::LowEnergyScanner> CreateScanner(bool extended) {
std::unique_ptr<hci::LowEnergyScanner> CreateScanner(
bool extended,
const hci::LowEnergyScanner::PacketFilterConfig& packet_filter_config) {
if (extended) {
return std::make_unique<hci::ExtendedLowEnergyScanner>(
le_address_manager_.get(), hci_, dispatcher_);
le_address_manager_.get(), packet_filter_config, hci_, dispatcher_);
}

return std::make_unique<hci::LegacyLowEnergyScanner>(
le_address_manager_.get(), hci_, dispatcher_);
le_address_manager_.get(), packet_filter_config, hci_, dispatcher_);
}

// Must be initialized first so that child nodes can be passed to other
Expand Down Expand Up @@ -1519,11 +1544,14 @@ void AdapterImpl::InitializeStep4() {
extended ? "yes" : "no");
hci_le_advertiser_ = CreateAdvertiser(extended);
hci_le_connector_ = CreateConnector(extended);
hci_le_scanner_ = CreateScanner(extended);

hci::LowEnergyScanner::PacketFilterConfig packet_filter_config =
GetPacketFilterConfig();
hci_le_scanner_ = CreateScanner(extended, packet_filter_config);

// Initialize the LE manager objects
le_discovery_manager_ = std::make_unique<LowEnergyDiscoveryManager>(
hci_le_scanner_.get(), &peer_cache_, dispatcher_);
hci_le_scanner_.get(), &peer_cache_, packet_filter_config, dispatcher_);
le_discovery_manager_->AttachInspect(
adapter_node_, kInspectLowEnergyDiscoveryManagerNodeName);
le_discovery_manager_->set_peer_connectable_callback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "pw_bluetooth_sapphire/internal/host/common/device_address.h"
#include "pw_bluetooth_sapphire/internal/host/common/macros.h"
#include "pw_bluetooth_sapphire/internal/host/common/random.h"
#include "pw_bluetooth_sapphire/internal/host/gap/fake_pairing_delegate.h"
#include "pw_bluetooth_sapphire/internal/host/gap/gap.h"
#include "pw_bluetooth_sapphire/internal/host/gap/low_energy_address_manager.h"
#include "pw_bluetooth_sapphire/internal/host/gap/peer.h"
Expand All @@ -41,18 +40,15 @@
#include "pw_bluetooth_sapphire/internal/host/hci/low_energy_connection.h"
#include "pw_bluetooth_sapphire/internal/host/hci/low_energy_connector.h"
#include "pw_bluetooth_sapphire/internal/host/l2cap/fake_channel.h"
#include "pw_bluetooth_sapphire/internal/host/l2cap/fake_channel_test.h"
#include "pw_bluetooth_sapphire/internal/host/l2cap/fake_l2cap.h"
#include "pw_bluetooth_sapphire/internal/host/l2cap/l2cap_defs.h"
#include "pw_bluetooth_sapphire/internal/host/l2cap/types.h"
#include "pw_bluetooth_sapphire/internal/host/sm/smp.h"
#include "pw_bluetooth_sapphire/internal/host/sm/test_security_manager.h"
#include "pw_bluetooth_sapphire/internal/host/sm/types.h"
#include "pw_bluetooth_sapphire/internal/host/testing/controller_test.h"
#include "pw_bluetooth_sapphire/internal/host/testing/fake_controller.h"
#include "pw_bluetooth_sapphire/internal/host/testing/fake_peer.h"
#include "pw_bluetooth_sapphire/internal/host/testing/inspect.h"
#include "pw_bluetooth_sapphire/internal/host/testing/test_packets.h"
#include "pw_bluetooth_sapphire/internal/host/transport/fake_acl_connection.h"

namespace bt::gap {
Expand All @@ -65,7 +61,6 @@ using bt::testing::FakeController;
using bt::testing::FakePeer;

using TestingBase = bt::testing::FakeDispatcherControllerTest<FakeController>;
using l2cap::testing::FakeChannel;
using TestSm = sm::testing::TestSecurityManager;
using TestSmFactory = sm::testing::TestSecurityManagerFactory;
using ConnectionResult = LowEnergyConnectionManager::ConnectionResult;
Expand Down Expand Up @@ -120,15 +115,20 @@ class LowEnergyConnectionManagerTest : public TestingBase {
gatt_ = std::make_unique<gatt::testing::FakeLayer>(dispatcher());
sm_factory_ = std::make_unique<TestSmFactory>();

hci::LowEnergyScanner::PacketFilterConfig packet_filter_config(false, 0);

address_manager_ = std::make_unique<LowEnergyAddressManager>(
kAdapterAddress,
/*delegate=*/[] { return false; },
cmd_weak,
dispatcher());
scanner_ = std::make_unique<hci::LegacyLowEnergyScanner>(
address_manager_.get(), transport()->GetWeakPtr(), dispatcher());
scanner_ =
std::make_unique<hci::LegacyLowEnergyScanner>(address_manager_.get(),
packet_filter_config,
transport()->GetWeakPtr(),
dispatcher());
discovery_manager_ = std::make_unique<LowEnergyDiscoveryManager>(
scanner_.get(), peer_cache_.get(), dispatcher());
scanner_.get(), peer_cache_.get(), packet_filter_config, dispatcher());
conn_mgr_ = std::make_unique<LowEnergyConnectionManager>(
transport()->GetWeakPtr(),
&addr_delegate_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "pw_bluetooth_sapphire/internal/host/gap/peer.h"
#include "pw_bluetooth_sapphire/internal/host/gap/peer_cache.h"
#include "pw_bluetooth_sapphire/internal/host/transport/transport.h"
#include "pw_bluetooth_sapphire/internal/host/hci/low_energy_scanner.h"

namespace bt::gap {

Expand Down Expand Up @@ -113,11 +113,13 @@ void LowEnergyDiscoverySession::Stop() {
LowEnergyDiscoveryManager::LowEnergyDiscoveryManager(
hci::LowEnergyScanner* scanner,
PeerCache* peer_cache,
const hci::LowEnergyScanner::PacketFilterConfig& packet_filter_config,
pw::async::Dispatcher& dispatcher)
: WeakSelf(this),
dispatcher_(dispatcher),
state_(State::kIdle, StateToString),
peer_cache_(peer_cache),
packet_filter_config_(packet_filter_config),
paused_count_(0),
scanner_(scanner) {
PW_DCHECK(peer_cache_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "pw_bluetooth_sapphire/internal/host/gap/peer_cache.h"
#include "pw_bluetooth_sapphire/internal/host/hci/fake_local_address_delegate.h"
#include "pw_bluetooth_sapphire/internal/host/hci/legacy_low_energy_scanner.h"
#include "pw_bluetooth_sapphire/internal/host/hci/low_energy_scanner.h"
#include "pw_bluetooth_sapphire/internal/host/testing/controller_test.h"
#include "pw_bluetooth_sapphire/internal/host/testing/fake_controller.h"
#include "pw_bluetooth_sapphire/internal/host/testing/fake_peer.h"
Expand Down Expand Up @@ -71,20 +72,24 @@ class LowEnergyDiscoveryManagerTest : public TestingBase {
settings.ApplyLegacyLEConfig();
test_device()->set_settings(settings);

hci::LowEnergyScanner::PacketFilterConfig packet_filter_config(false, 0);

// TODO(armansito): Now that the hci::LowEnergyScanner is injected into
// |discovery_manager_| rather than constructed by it, a fake implementation
// could be injected directly. Consider providing fake behavior here in this
// harness rather than using a FakeController.
scanner_ = std::make_unique<hci::LegacyLowEnergyScanner>(
&fake_address_delegate_, transport()->GetWeakPtr(), dispatcher());
scanner_ =
std::make_unique<hci::LegacyLowEnergyScanner>(&fake_address_delegate_,
packet_filter_config,
transport()->GetWeakPtr(),
dispatcher());
discovery_manager_ = std::make_unique<LowEnergyDiscoveryManager>(
scanner_.get(), &peer_cache_, dispatcher());
scanner_.get(), &peer_cache_, packet_filter_config, dispatcher());
discovery_manager_->AttachInspect(inspector_.GetRoot(), kInspectNodeName);

test_device()->set_scan_state_callback(
std::bind(&LowEnergyDiscoveryManagerTest::OnScanStateChanged,
this,
std::placeholders::_1));
test_device()->set_scan_state_callback([this](auto&& PH1) {
OnScanStateChanged(std::forward<decltype(PH1)>(PH1));
});
}

void TearDown() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AndroidVendorCapabilities final {
// Number of IRK entries supported in the firmware
uint8_t irk_list_size() const { return irk_list_size_; }

// Support for filtering in the controller
// Support for packet filtering in the controller
bool supports_filtering() const { return supports_filtering_; }

// Number of filters supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
#include <pw_async/heap_dispatcher.h>

#include <memory>
#include <queue>
#include <unordered_set>

#include "pw_bluetooth_sapphire/internal/host/common/byte_buffer.h"
#include "pw_bluetooth_sapphire/internal/host/common/device_address.h"
#include "pw_bluetooth_sapphire/internal/host/common/inspectable.h"
#include "pw_bluetooth_sapphire/internal/host/common/weak_self.h"
#include "pw_bluetooth_sapphire/internal/host/gap/discovery_filter.h"
Expand Down Expand Up @@ -120,9 +117,11 @@ class LowEnergyDiscoveryManager final
public WeakSelf<LowEnergyDiscoveryManager> {
public:
// |peer_cache| and |scanner| MUST out-live this LowEnergyDiscoveryManager.
LowEnergyDiscoveryManager(hci::LowEnergyScanner* scanner,
PeerCache* peer_cache,
pw::async::Dispatcher& dispatcher);
LowEnergyDiscoveryManager(
hci::LowEnergyScanner* scanner,
PeerCache* peer_cache,
const hci::LowEnergyScanner::PacketFilterConfig& packet_filter_config,
pw::async::Dispatcher& dispatcher);
~LowEnergyDiscoveryManager() override;

// Starts a new discovery session and reports the result via |callback|. If a
Expand Down Expand Up @@ -245,6 +244,8 @@ class LowEnergyDiscoveryManager final
// hold a raw pointer as we expect this to out-live us.
PeerCache* const peer_cache_;

hci::LowEnergyScanner::PacketFilterConfig packet_filter_config_;

// Called when a directed connectable advertisement is received during an
// active or passive scan.
PeerConnectableCallback connectable_cb_;
Expand Down
9 changes: 7 additions & 2 deletions pw_bluetooth_sapphire/host/hci/extended_low_energy_scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <pw_assert/check.h>
#include <pw_bluetooth/hci_common.emb.h>

#include "pw_bluetooth_sapphire/internal/host/hci/low_energy_scanner.h"

namespace bt::hci {

using pw::bluetooth::emboss::BdAddrView;
Expand All @@ -34,10 +36,13 @@ using pw::bluetooth::emboss::MakeLEExtendedAdvertisingReportDataView;

ExtendedLowEnergyScanner::ExtendedLowEnergyScanner(
LocalAddressDelegate* local_addr_delegate,
const PacketFilterConfig& packet_filter_config,
Transport::WeakPtr transport,
pw::async::Dispatcher& pw_dispatcher)
: LowEnergyScanner(
local_addr_delegate, std::move(transport), pw_dispatcher) {
: LowEnergyScanner(local_addr_delegate,
packet_filter_config,
std::move(transport),
pw_dispatcher) {
event_handler_id_ = hci()->command_channel()->AddLEMetaEventHandler(
hci_spec::kLEExtendedAdvertisingReportSubeventCode,
[this](const EventPacket& event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class ExtendedLowEnergyScannerTest : public TestingBase,
test_device()->set_settings(settings);

scanner_ = std::make_unique<ExtendedLowEnergyScanner>(
&fake_address_delegate_, transport()->GetWeakPtr(), dispatcher());
&fake_address_delegate_,
ExtendedLowEnergyScanner::PacketFilterConfig(false, 0),
transport()->GetWeakPtr(),
dispatcher());
scanner_->set_delegate(this);

auto p = std::make_unique<FakePeer>(kPublicAddr1, dispatcher(), true, true);
Expand Down
9 changes: 5 additions & 4 deletions pw_bluetooth_sapphire/host/hci/legacy_low_energy_scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
#include <pw_assert/check.h>
#include <pw_preprocessor/compiler.h>

#include "pw_bluetooth_sapphire/internal/host/hci/advertising_report_parser.h"

namespace bt::hci {
namespace pwemb = pw::bluetooth::emboss;

LegacyLowEnergyScanner::LegacyLowEnergyScanner(
LocalAddressDelegate* local_addr_delegate,
const PacketFilterConfig& packet_filter_config,
Transport::WeakPtr transport,
pw::async::Dispatcher& pw_dispatcher)
: LowEnergyScanner(
local_addr_delegate, std::move(transport), pw_dispatcher),
: LowEnergyScanner(local_addr_delegate,
packet_filter_config,
std::move(transport),
pw_dispatcher),
weak_self_(this) {
auto self = weak_self_.GetWeakPtr();
event_handler_id_ = hci()->command_channel()->AddLEMetaEventHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ class LegacyLowEnergyScannerTest : public TestingBase,
test_device()->set_settings(settings);

scanner_ = std::make_unique<LegacyLowEnergyScanner>(
fake_address_delegate(), transport()->GetWeakPtr(), dispatcher());
fake_address_delegate(),
LegacyLowEnergyScanner::PacketFilterConfig(false, 0),
transport()->GetWeakPtr(),
dispatcher());
scanner_->set_delegate(this);

auto p = std::make_unique<FakePeer>(kPublicAddr,
Expand Down
9 changes: 6 additions & 3 deletions pw_bluetooth_sapphire/host/hci/low_energy_scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ LowEnergyScanner::PendingScanResult::PendingScanResult(
StartTimer();
}

LowEnergyScanner::LowEnergyScanner(LocalAddressDelegate* local_addr_delegate,
hci::Transport::WeakPtr hci,
pw::async::Dispatcher& pw_dispatcher)
LowEnergyScanner::LowEnergyScanner(
LocalAddressDelegate* local_addr_delegate,
const PacketFilterConfig& packet_filter_config,
hci::Transport::WeakPtr hci,
pw::async::Dispatcher& pw_dispatcher)
: pw_dispatcher_(pw_dispatcher),
scan_timeout_task_(pw_dispatcher_),
packet_filter_config_(packet_filter_config),
local_addr_delegate_(local_addr_delegate),
hci_(std::move(hci)) {
PW_DCHECK(local_addr_delegate_);
Expand Down
12 changes: 8 additions & 4 deletions pw_bluetooth_sapphire/host/hci/low_energy_scanner_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,18 @@ class LowEnergyScannerTest : public TestingBase,

template <bool same = std::is_same_v<T, ExtendedLowEnergyScanner>>
std::enable_if_t<same, ExtendedLowEnergyScanner>* CreateScannerInternal() {
return new ExtendedLowEnergyScanner(
fake_address_delegate(), transport()->GetWeakPtr(), dispatcher());
return new ExtendedLowEnergyScanner(fake_address_delegate(),
{false, 0},
transport()->GetWeakPtr(),
dispatcher());
}

template <bool same = std::is_same_v<T, LegacyLowEnergyScanner>>
std::enable_if_t<same, LegacyLowEnergyScanner>* CreateScannerInternal() {
return new LegacyLowEnergyScanner(
fake_address_delegate(), transport()->GetWeakPtr(), dispatcher());
return new LegacyLowEnergyScanner(fake_address_delegate(),
{false, 0},
transport()->GetWeakPtr(),
dispatcher());
}

using PeerFoundCallback = fit::function<void(const LowEnergyScanResult&)>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace bt::hci {
class ExtendedLowEnergyScanner final : public LowEnergyScanner {
public:
ExtendedLowEnergyScanner(LocalAddressDelegate* local_addr_delegate,
const PacketFilterConfig& packet_filter_config,
Transport::WeakPtr transport,
pw::async::Dispatcher& pw_dispatcher);
~ExtendedLowEnergyScanner() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class LocalAddressDelegate;
class LegacyLowEnergyScanner final : public LowEnergyScanner {
public:
LegacyLowEnergyScanner(LocalAddressDelegate* local_addr_delegate,
const PacketFilterConfig& packet_filter_config,
Transport::WeakPtr transport,
pw::async::Dispatcher& pw_dispatcher);
~LegacyLowEnergyScanner() override;
Expand Down
Loading

0 comments on commit 5b8a5c5

Please sign in to comment.