C/C++ API Reference
Loading...
Searching...
No Matches
central.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#pragma once
16
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"
21
24
26
29 public:
30 // The maximum number of scan results to queue in each ScanHandle
31 static constexpr uint8_t kMaxScanResultsQueueSize = 10;
32
33 // TODO: https://pwbug.dev/377301546 - Don't expose Adapter in public API.
36 Central(bt::gap::Adapter::WeakPtr adapter,
37 pw::async::Dispatcher& dispatcher,
39 ~Central() override;
40
42 pw::bluetooth::PeerId peer_id,
44 PW_LOCKS_EXCLUDED(lock());
45
47 const ScanOptions& options) override PW_LOCKS_EXCLUDED(lock());
48
49 static pw::sync::Mutex& lock();
50
51 private:
52 class ScanHandleImpl final : public ScanHandle {
53 public:
54 explicit ScanHandleImpl(uint16_t scan_id, Central* central)
55 : scan_id_(scan_id), central_(central) {}
56
57 // Synchronously clears ScanState's pointer to this object and
58 // asynchronously stops the scan procedure.
59 ~ScanHandleImpl() override;
60
61 void QueueScanResultLocked(ScanResult&& result)
63
64 void WakeLocked() PW_EXCLUSIVE_LOCKS_REQUIRED(lock()) {
65 std::move(waker_).Wake();
66 }
67
68 void OnScanErrorLocked() PW_EXCLUSIVE_LOCKS_REQUIRED(lock()) {
69 central_ = nullptr;
70 WakeLocked();
71 }
72
73 private:
74 async2::PollResult<ScanResult> PendResult(async2::Context& cx) override;
75
76 // std::unique_ptr custom Deleter
77 void Release() override { delete this; }
78
79 const uint16_t scan_id_;
80
81 // Set to null when Central is destroyed or scanning has stopped.
82 Central* central_ PW_GUARDED_BY(lock());
83
84 // PendResult() waker. Set when Pending was returned.
85 async2::Waker waker_ PW_GUARDED_BY(lock());
86 std::queue<ScanResult> results_ PW_GUARDED_BY(lock());
87 };
88
89 // Created and destroyed on the Bluetooth thread.
90 class ScanState {
91 public:
92 // Must be run on Bluetooth thread. Not thread safe.
93 explicit ScanState(
94 std::unique_ptr<bt::gap::LowEnergyDiscoverySession> session,
95 ScanHandleImpl* scan_handle,
96 uint16_t scan_id,
97 Central* central);
98
99 ~ScanState();
100
101 void OnScanHandleDestroyedLocked() PW_EXCLUSIVE_LOCKS_REQUIRED(lock()) {
102 scan_handle_ = nullptr;
103 }
104
105 private:
106 // Must be run on Bluetooth thread. Not thread safe.
107 void OnScanResult(const bt::gap::Peer& peer) PW_LOCKS_EXCLUDED(lock());
108
109 // Must be run on Bluetooth thread. Not thread safe.
110 void OnError() PW_LOCKS_EXCLUDED(lock());
111
112 const uint16_t scan_id_;
113
114 // Set to null synchronously when ScanHandleImpl is destroyed.
115 ScanHandleImpl* scan_handle_ PW_GUARDED_BY(lock());
116
117 // Members must only be accessed on Bluetooth thread.
118 Central* const central_;
119 std::unique_ptr<bt::gap::LowEnergyDiscoverySession> session_;
120 };
121
122 // Asynchronously stops the scan corresponding to `scan_id` and synchronously
123 // clears `ScanState.scan_handle_`.
124 void StopScanLocked(uint16_t scan_id) PW_EXCLUSIVE_LOCKS_REQUIRED(lock());
125
126 void OnConnectionResult(bt::PeerId peer_id,
127 bt::gap::Adapter::LowEnergy::ConnectionResult result,
128 async2::OnceSender<ConnectResult> result_sender)
129 PW_LOCKS_EXCLUDED(lock());
130
131 std::unordered_map<uint16_t, ScanState> scans_ PW_GUARDED_BY(lock());
132
133 // Must only be used on the Bluetooth thread.
134 bt::gap::Adapter::WeakPtr adapter_;
135
136 // Dispatcher for Bluetooth thread. Thread safe.
137 pw::async::Dispatcher& dispatcher_;
138 pw::async::HeapDispatcher heap_dispatcher_;
139
140 pw::multibuf::MultiBufAllocator& allocator_;
141
142 // Must only be used on the Bluetooth thread.
143 WeakSelf<Central> weak_factory_{this};
144
145 // Thread safe to copy and destroy, but WeakPtr should only be used
146 // or dereferenced on the Bluetooth thread (WeakRef is not thread safe).
147 WeakSelf<Central>::WeakPtr self_{weak_factory_.GetWeakPtr()};
148};
149
150} // namespace pw::bluetooth_sapphire
Definition: once_sender.h:43
Definition: dispatcher.h:48
Represents an ongoing LE scan.
Definition: central2.h:148
Represents the LE central role. Used to scan and connect to peripherals.
Definition: central2.h:31
Must only be constructed and destroyed on the Bluetooth thread.
Definition: central.h:28
Definition: allocator.h:57
Definition: mutex.h:40
async2::OnceReceiver< ScanStartResult > Scan(const ScanOptions &options) override
Central(bt::gap::Adapter::WeakPtr adapter, pw::async::Dispatcher &dispatcher, pw::multibuf::MultiBufAllocator &allocator)
pw::expected< Connection2::Ptr, ConnectError > ConnectResult
The result type returned by Connect().
Definition: central2.h:205
#define PW_GUARDED_BY(x)
Definition: lock_annotations.h:60
#define PW_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: lock_annotations.h:146
#define PW_LOCKS_EXCLUDED(...)
Definition: lock_annotations.h:176
Dual-mode Bluetooth host stack.
Definition: central.h:23
The Pigweed namespace.
Definition: alignment.h:27
Parameters used during a scan.
Definition: central2.h:89
Represents parameters that are set on a per-connection basis.
Definition: connection2.h:111