xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/host/gap/low_energy_connection_handle.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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 #include "pw_bluetooth_sapphire/internal/host/gap/low_energy_connection_handle.h"
16 
17 #include "pw_bluetooth_sapphire/internal/host/gap/low_energy_connection.h"
18 #include "pw_bluetooth_sapphire/internal/host/gap/low_energy_connection_manager.h"
19 
20 namespace bt::gap {
21 
LowEnergyConnectionHandle(PeerId peer_id,hci_spec::ConnectionHandle handle,fit::callback<void (LowEnergyConnectionHandle *)> release_cb,AcceptCisCallback accept_cis_cb,fit::function<sm::BondableMode ()> bondable_cb,fit::function<sm::SecurityProperties ()> security_cb,fit::function<pw::bluetooth::emboss::ConnectionRole ()> role_cb)22 LowEnergyConnectionHandle::LowEnergyConnectionHandle(
23     PeerId peer_id,
24     hci_spec::ConnectionHandle handle,
25     fit::callback<void(LowEnergyConnectionHandle*)> release_cb,
26     AcceptCisCallback accept_cis_cb,
27     fit::function<sm::BondableMode()> bondable_cb,
28     fit::function<sm::SecurityProperties()> security_cb,
29     fit::function<pw::bluetooth::emboss::ConnectionRole()> role_cb)
30     : active_(true),
31       peer_id_(peer_id),
32       handle_(handle),
33       release_cb_(std::move(release_cb)),
34       accept_cis_cb_(std::move(accept_cis_cb)),
35       bondable_cb_(std::move(bondable_cb)),
36       security_cb_(std::move(security_cb)),
37       role_cb_(std::move(role_cb)) {
38   PW_CHECK(peer_id_.IsValid());
39 }
40 
~LowEnergyConnectionHandle()41 LowEnergyConnectionHandle::~LowEnergyConnectionHandle() {
42   if (active_) {
43     Release();
44   }
45 }
46 
Release()47 void LowEnergyConnectionHandle::Release() {
48   PW_CHECK(active_);
49   active_ = false;
50   if (release_cb_) {
51     release_cb_(this);
52   }
53 }
54 
MarkClosed()55 void LowEnergyConnectionHandle::MarkClosed() {
56   active_ = false;
57   if (closed_cb_) {
58     // Move the callback out of |closed_cb_| to prevent it from deleting itself
59     // by deleting |this|.
60     auto f = std::move(closed_cb_);
61     f();
62   }
63 }
64 
AcceptCis(iso::CigCisIdentifier id,iso::CisEstablishedCallback cis_established_cb)65 iso::AcceptCisStatus LowEnergyConnectionHandle::AcceptCis(
66     iso::CigCisIdentifier id, iso::CisEstablishedCallback cis_established_cb) {
67   PW_CHECK(active_);
68   return accept_cis_cb_(id, std::move(cis_established_cb));
69 }
70 
bondable_mode() const71 sm::BondableMode LowEnergyConnectionHandle::bondable_mode() const {
72   PW_CHECK(active_);
73   return bondable_cb_();
74 }
75 
security() const76 sm::SecurityProperties LowEnergyConnectionHandle::security() const {
77   PW_CHECK(active_);
78   return security_cb_();
79 }
80 
role() const81 pw::bluetooth::emboss::ConnectionRole LowEnergyConnectionHandle::role() const {
82   PW_CHECK(active_);
83   return role_cb_();
84 }
85 
86 }  // namespace bt::gap
87