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