17#include "pw_bluetooth/low_energy/central2.h"
18#include "pw_bluetooth_sapphire/internal/connection.h"
19#include "pw_bluetooth_sapphire/internal/host/gap/adapter.h"
20#include "pw_multibuf/allocator.h"
22namespace pw::bluetooth_sapphire {
28 static constexpr uint8_t kMaxScanResultsQueueSize = 10;
33 Central(bt::gap::Adapter::WeakPtr adapter,
39 pw::bluetooth::PeerId peer_id,
41 PW_LOCKS_EXCLUDED(lock());
44 const ScanOptions& options)
override PW_LOCKS_EXCLUDED(lock());
49 class ScanHandleImpl final :
public ScanHandle {
51 explicit ScanHandleImpl(uint16_t scan_id,
Central* central)
52 : scan_id_(scan_id), central_(central) {}
56 ~ScanHandleImpl()
override;
58 void QueueScanResultLocked(ScanResult&& result)
59 PW_EXCLUSIVE_LOCKS_REQUIRED(lock());
61 void WakeLocked() PW_EXCLUSIVE_LOCKS_REQUIRED(lock()) {
62 std::move(waker_).Wake();
65 void OnScanErrorLocked() PW_EXCLUSIVE_LOCKS_REQUIRED(lock()) {
71 async2::Poll<pw::Result<ScanResult>> PendResult(
72 async2::Context& cx)
override;
75 void Release()
override {
delete this; }
77 const uint16_t scan_id_;
80 Central* central_ PW_GUARDED_BY(lock());
83 async2::Waker waker_ PW_GUARDED_BY(lock());
84 std::queue<ScanResult> results_ PW_GUARDED_BY(lock());
92 std::unique_ptr<bt::gap::LowEnergyDiscoverySession> session,
93 ScanHandleImpl* scan_handle,
99 void OnScanHandleDestroyedLocked() PW_EXCLUSIVE_LOCKS_REQUIRED(lock()) {
100 scan_handle_ =
nullptr;
105 void OnScanResult(
const bt::gap::Peer& peer) PW_LOCKS_EXCLUDED(lock());
108 void OnError() PW_LOCKS_EXCLUDED(lock());
110 const uint16_t scan_id_;
113 ScanHandleImpl* scan_handle_ PW_GUARDED_BY(lock());
116 Central* const central_;
117 std::unique_ptr<bt::gap::LowEnergyDiscoverySession> session_;
122 void StopScanLocked(uint16_t scan_id) PW_EXCLUSIVE_LOCKS_REQUIRED(lock());
124 void OnConnectionResult(bt::PeerId peer_id,
125 bt::gap::Adapter::LowEnergy::ConnectionResult result,
127 PW_LOCKS_EXCLUDED(lock());
129 std::unordered_map<uint16_t, ScanState> scans_ PW_GUARDED_BY(lock());
132 bt::gap::Adapter::WeakPtr adapter_;
135 pw::async::Dispatcher& dispatcher_;
136 pw::async::HeapDispatcher heap_dispatcher_;
138 pw::multibuf::MultiBufAllocator& allocator_;
141 WeakSelf<Central> weak_factory_{
this};
145 WeakSelf<Central>::WeakPtr self_{weak_factory_.GetWeakPtr()};
Definition: once_sender.h:41
Definition: dispatcher.h:45
Represents an ongoing LE scan.
Definition: central2.h:146
Represents the LE central role. Used to scan and connect to peripherals.
Definition: central2.h:29
pw::expected< Connection2::Ptr, ConnectError > ConnectResult
The result type returned by Connect().
Definition: central2.h:204
Must only be constructed and destroyed on the Bluetooth thread.
Definition: central.h:25
async2::OnceReceiver< ScanStartResult > Scan(const ScanOptions &options) override
Central(bt::gap::Adapter::WeakPtr adapter, pw::async::Dispatcher &dispatcher, pw::multibuf::MultiBufAllocator &allocator)
Definition: allocator.h:54
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27
Parameters used during a scan.
Definition: central2.h:87
Represents parameters that are set on a per-connection basis.
Definition: connection2.h:109