Skip to content

Commit

Permalink
pw_bluetooth_sapphire: Move scan_id into LowEnergyDiscoverySession
Browse files Browse the repository at this point in the history
Bug: 42174576
Test: bazelisk test --config googletest //pw_bluetooth_sapphire/host/{common:common_test,hci:hci_test,gap:gap_test}
Change-Id: I9e233abff07a88dc96d5ab3962a4a9de1a3fd2b9
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/260220
Reviewed-by: Ben Lawson <[email protected]>
Commit-Queue: Faraaz Sareshwala <[email protected]>
Lint: Lint 🤖 <[email protected]>
Docs-Not-Needed: Faraaz Sareshwala <[email protected]>
  • Loading branch information
Faraaz Sareshwala authored and CQ Bot Account committed Feb 10, 2025
1 parent 5b8a5c5 commit 17a5e76
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
7 changes: 3 additions & 4 deletions pw_bluetooth_sapphire/central.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,16 @@ async2::OnceReceiver<Central::ScanStartResult> Central::Scan(
return;
}

uint16_t scan_id = self->next_scan_id_++;
ScanHandleImpl* scan_handle_raw_ptr =
new ScanHandleImpl(scan_id, &self.get());
new ScanHandleImpl(session->scan_id(), &self.get());
ScanHandle::Ptr scan_handle_ptr(scan_handle_raw_ptr);
{
std::lock_guard guard(lock());
auto [iter, emplaced] = self->scans_.try_emplace(scan_id,
auto [iter, emplaced] = self->scans_.try_emplace(session->scan_id(),
std::move(session),
std::move(filters),
scan_handle_raw_ptr,
scan_id,
session->scan_id(),
&self.get());
PW_CHECK(emplaced);
}
Expand Down
1 change: 1 addition & 0 deletions pw_bluetooth_sapphire/host/gap/fake_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ void FakeAdapter::FakeLowEnergy::StartAdvertising(
void FakeAdapter::FakeLowEnergy::StartDiscovery(bool active,
SessionCallback callback) {
auto session = std::make_unique<LowEnergyDiscoverySession>(
next_scan_id_++,
active,
*adapter_->peer_cache(),
adapter_->pw_dispatcher_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ const char* kInspectScanIntervalPropertyName = "scan_interval_ms";
const char* kInspectScanWindowPropertyName = "scan_window_ms";

LowEnergyDiscoverySession::LowEnergyDiscoverySession(
uint16_t scan_id,
bool active,
PeerCache& peer_cache,
pw::async::Dispatcher& dispatcher,
fit::function<void(LowEnergyDiscoverySession*)> on_stop_cb,
fit::function<const std::unordered_set<PeerId>&()> cached_scan_results_fn)
: WeakSelf(this),
scan_id_(scan_id),
active_(active),
peer_cache_(peer_cache),
heap_dispatcher_(dispatcher),
Expand Down Expand Up @@ -260,6 +262,7 @@ LowEnergyDiscoveryManager::AddSession(bool active) {
return this->cached_scan_results_;
};
auto session = std::make_unique<LowEnergyDiscoverySession>(
next_scan_id_++,
active,
*peer_cache_,
dispatcher_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class FakeAdapter final : public Adapter {
pw::chrono::SystemClock::duration) override {}

private:
uint16_t next_scan_id_ = 0;
FakeAdapter* adapter_;
AdvertisementId next_advertisement_id_ = AdvertisementId(1);
std::unordered_map<AdvertisementId, RegisteredAdvertisement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ class LowEnergyDiscoveryManager final
// hold a raw pointer as we expect this to out-live us.
PeerCache* const peer_cache_;

uint16_t next_scan_id_ = 0;
hci::LowEnergyScanner::PacketFilterConfig packet_filter_config_;

// Called when a directed connectable advertisement is received during an
Expand Down Expand Up @@ -292,6 +293,7 @@ class LowEnergyDiscoverySession final
: public WeakSelf<LowEnergyDiscoverySession> {
public:
explicit LowEnergyDiscoverySession(
uint16_t scan_id,
bool active,
PeerCache& peer_cache,
pw::async::Dispatcher& dispatcher,
Expand Down Expand Up @@ -343,12 +345,15 @@ class LowEnergyDiscoverySession final
// Returns true if this session has not been stopped and has not errored.
bool alive() const { return alive_; }

uint16_t scan_id() const { return scan_id_; }

// Returns true if this is an active discovery session, or false if this is a
// passive discovery session.
bool active() const { return active_; }

private:
bool alive_{true};
uint16_t scan_id_;
bool alive_ = true;
bool active_;
PeerCache& peer_cache_;
pw::async::HeapDispatcher heap_dispatcher_;
Expand Down
3 changes: 0 additions & 3 deletions pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/central.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ class Central final : public pw::bluetooth::low_energy::Central2 {
// clears `ScanState.scan_handle_`.
void StopScanLocked(uint16_t scan_id) PW_EXCLUSIVE_LOCKS_REQUIRED(lock());

// Must only be used on the Bluetooth thread.
uint16_t next_scan_id_ = 0;

std::unordered_map<uint16_t, ScanState> scans_ PW_GUARDED_BY(lock());

// Must only be used on the Bluetooth thread.
Expand Down

0 comments on commit 17a5e76

Please sign in to comment.