Pigweed
 
Loading...
Searching...
No Matches
central2.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#pragma once
15
16#include <optional>
17
18#include "pw_async2/once_sender.h"
19#include "pw_bluetooth/internal/raii_ptr.h"
20#include "pw_bluetooth/low_energy/connection2.h"
21#include "pw_bluetooth/low_energy/phy.h"
22#include "pw_bluetooth/types.h"
23#include "pw_chrono/system_clock.h"
24#include "pw_result/expected.h"
25
26namespace pw::bluetooth::low_energy {
27
29class Central2 {
30 public:
33 struct ScanFilter {
35 std::optional<Uuid> service_uuid;
36
38 std::optional<Uuid> service_data_uuid;
39
46 std::optional<uint16_t> manufacturer_id;
47
52 std::optional<bool> connectable;
53
57 std::optional<std::string_view> name;
58
70 std::optional<int8_t> max_path_loss;
71
73 std::optional<Uuid> solicitation_uuid;
74 };
75
76 enum class ScanType : uint8_t {
77 kPassive,
84 };
85
87 struct ScanOptions {
93 pw::span<const ScanFilter> filters;
94
98 uint16_t interval;
99
104 uint16_t window;
105
109
112 Phy phys = Phy::k1Megabit;
113 };
114
115 struct ScanResult {
117 PeerId peer_id;
118
122
129 std::optional<uint8_t> rssi;
130
133
139 std::optional<InlineString<22>> name;
140
142 chrono::SystemClock::time_point last_updated;
143 };
144
147 public:
149 virtual ~ScanHandle() = default;
150
164 async2::Context& cx) = 0;
165
166 private:
169 virtual void Release() = 0;
170
171 public:
174 using Ptr = internal::RaiiPtr<ScanHandle, &ScanHandle::Release>;
175 };
176
178 enum class ConnectError : uint8_t {
181
184
187
192 };
193
194 enum class StartScanError : uint8_t {
200 kInternal,
201 };
202
204 using ConnectResult = pw::expected<Connection2::Ptr, ConnectError>;
205
207 using ScanStartResult = pw::expected<ScanHandle::Ptr, StartScanError>;
208
209 virtual ~Central2() = default;
210
227 PeerId peer_id, Connection2::ConnectionOptions options) = 0;
228
244 const ScanOptions& options) = 0;
245};
246
247} // namespace pw::bluetooth::low_energy
Definition: dispatcher_base.h:52
Definition: once_sender.h:41
Definition: poll.h:54
Represents an ongoing LE scan.
Definition: central2.h:146
virtual async2::Poll< pw::Result< ScanResult > > PendResult(async2::Context &cx)=0
virtual ~ScanHandle()=default
Stops the scan.
internal::RaiiPtr< ScanHandle, &ScanHandle::Release > Ptr
Definition: central2.h:174
Represents the LE central role. Used to scan and connect to peripherals.
Definition: central2.h:29
virtual async2::OnceReceiver< ConnectResult > Connect(PeerId peer_id, Connection2::ConnectionOptions options)=0
ScanType
Definition: central2.h:76
@ kActiveUseRandomAddress
Send scanning PDUs with the random address.
@ kActiveUsePublicAddress
Send scanning PDUs with the public address.
@ kActiveUseResolvablePrivateAddress
Send scanning PDUs with a generated Resolvable Private Address.
ConnectError
Possible errors returned by Connect.
Definition: central2.h:178
@ kInvalidOptions
The ConnectionOptions were invalid.
@ kAlreadyExists
A connection to the peer already exists.
pw::expected< ScanHandle::Ptr, StartScanError > ScanStartResult
The result type returned by Scan().
Definition: central2.h:207
virtual async2::OnceReceiver< ScanStartResult > Scan(const ScanOptions &options)=0
pw::expected< Connection2::Ptr, ConnectError > ConnectResult
The result type returned by Connect().
Definition: central2.h:204
StartScanError
Definition: central2.h:194
@ kInvalidParameters
Some of the scan options are invalid.
@ kScanInProgress
A scan is already in progress. Only 1 scan may be active at a time.
@ kInternal
An internal error occurred and a scan could not be started.
Definition: multibuf.h:245
std::optional< Uuid > service_data_uuid
Filter based on service data containing the given UUID.
Definition: central2.h:38
std::optional< Uuid > service_uuid
Filter based on advertised service UUID.
Definition: central2.h:35
std::optional< Uuid > solicitation_uuid
Require that a peer solicits support for a service UUID.
Definition: central2.h:73
std::optional< uint16_t > manufacturer_id
Definition: central2.h:46
std::optional< bool > connectable
Definition: central2.h:52
std::optional< int8_t > max_path_loss
Definition: central2.h:70
std::optional< std::string_view > name
Definition: central2.h:57
Parameters used during a scan.
Definition: central2.h:87
pw::span< const ScanFilter > filters
Definition: central2.h:93
uint16_t window
Definition: central2.h:104
uint16_t interval
Definition: central2.h:98
ScanType scan_type
Definition: central2.h:108
PeerId peer_id
Uniquely identifies this peer on the current system.
Definition: central2.h:117
bool connectable
Definition: central2.h:121
chrono::SystemClock::time_point last_updated
Timestamp of when the information in this ScanResult was last updated.
Definition: central2.h:142
pw::multibuf::MultiBuf data
This contains the advertising data last received from the peer.
Definition: central2.h:132
std::optional< InlineString< 22 > > name
Definition: central2.h:139
std::optional< uint8_t > rssi
Definition: central2.h:129
Represents parameters that are set on a per-connection basis.
Definition: connection2.h:109