17#include "pw_bluetooth/low_energy/peripheral2.h"
18#include "pw_bluetooth_sapphire/internal/host/gap/adapter.h"
20namespace pw::bluetooth_sapphire {
37 PW_LOCKS_EXCLUDED(lock());
46 class AdvertisedPeripheralImpl final
49 AdvertisedPeripheralImpl(bt::gap::AdvertisementId
id,
51 : id_(id), peripheral_(peripheral) {}
52 ~AdvertisedPeripheralImpl()
override {
53 std::lock_guard lock(Peripheral::lock());
55 peripheral_->OnAdvertisedPeripheralDestroyedLocked(id_);
60 async2::Poll<pw::bluetooth::low_energy::Connection2::Ptr> PendConnection(
61 [[maybe_unused]] async2::Context& cx)
override {
63 return async2::Pending();
65 void StopAdvertising()
override;
66 async2::Poll<pw::Status> PendStop(async2::Context& cx)
override;
67 void Release()
override {
delete this; }
70 friend class Advertisement;
72 const bt::gap::AdvertisementId id_;
75 Peripheral* peripheral_ PW_GUARDED_BY(lock());
77 std::optional<pw::Status> stop_status_ PW_GUARDED_BY(lock());
80 async2::Waker waker_ PW_GUARDED_BY(lock());
84 class Advertisement final {
86 Advertisement(bt::gap::AdvertisementInstance&& instance,
87 AdvertisedPeripheralImpl* advertised_peripheral)
88 : advertised_peripheral_(advertised_peripheral),
89 instance_(std::move(instance)) {}
94 void OnAdvertisedPeripheralDestroyedLocked()
95 PW_EXCLUSIVE_LOCKS_REQUIRED(lock()) {
96 advertised_peripheral_ =
nullptr;
99 void OnStopLocked(
pw::Status status) PW_EXCLUSIVE_LOCKS_REQUIRED(lock());
102 friend class AdvertisedPeripheralImpl;
104 AdvertisedPeripheralImpl* advertised_peripheral_;
107 bt::gap::AdvertisementInstance instance_;
111 void OnAdvertisedPeripheralDestroyedLocked(
112 bt::gap::AdvertisementId advertisement_id)
113 PW_EXCLUSIVE_LOCKS_REQUIRED(lock());
117 void StopAdvertising(bt::gap::AdvertisementId advertisement_id);
120 void OnAdvertiseResult(bt::gap::AdvertisementInstance instance,
121 bt::hci::Result<> result,
122 async2::OnceSender<AdvertiseResult> result_sender);
125 void OnConnection(bt::gap::AdvertisementId advertisement_id,
126 bt::gap::Adapter::LowEnergy::ConnectionResult result);
132 bt::gap::Adapter::WeakPtr adapter_;
134 std::unordered_map<bt::gap::AdvertisementId, Advertisement> advertisements_
135 PW_GUARDED_BY(lock());
138 WeakSelf<Peripheral> weak_factory_{
this};
142 WeakSelf<Peripheral>::WeakPtr self_{weak_factory_.GetWeakPtr()};
Definition: once_sender.h:41
Definition: dispatcher.h:45
Definition: heap_dispatcher.h:23
AdvertisedPeripheral instances are valid for the duration of advertising.
Definition: peripheral2.h:32
Represents the LE Peripheral role, which advertises and is connected to.
Definition: peripheral2.h:91
Must only be constructed and destroyed on the Bluetooth thread.
Definition: peripheral.h:23
async2::OnceReceiver< AdvertiseResult > Advertise(const AdvertisingParameters ¶meters) override
~Peripheral() override
Must only be destroyed on the Bluetooth thread.
Peripheral(bt::gap::Adapter::WeakPtr adapter, pw::async::Dispatcher &dispatcher)
Must only be constructed on the Bluetooth thread.
Represents the parameters for configuring advertisements.
Definition: peripheral2.h:161