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,fit::function<sm::BondableMode ()> bondable_cb,fit::function<sm::SecurityProperties ()> security_cb)22 LowEnergyConnectionHandle::LowEnergyConnectionHandle(
23 PeerId peer_id,
24 hci_spec::ConnectionHandle handle,
25 fit::callback<void(LowEnergyConnectionHandle*)> release_cb,
26 fit::function<sm::BondableMode()> bondable_cb,
27 fit::function<sm::SecurityProperties()> security_cb)
28 : active_(true),
29 peer_id_(peer_id),
30 handle_(handle),
31 release_cb_(std::move(release_cb)),
32 bondable_cb_(std::move(bondable_cb)),
33 security_cb_(std::move(security_cb)) {
34 BT_ASSERT(peer_id_.IsValid());
35 }
36
~LowEnergyConnectionHandle()37 LowEnergyConnectionHandle::~LowEnergyConnectionHandle() {
38 if (active_) {
39 Release();
40 }
41 }
42
Release()43 void LowEnergyConnectionHandle::Release() {
44 BT_ASSERT(active_);
45 active_ = false;
46 if (release_cb_) {
47 release_cb_(this);
48 }
49 }
50
MarkClosed()51 void LowEnergyConnectionHandle::MarkClosed() {
52 active_ = false;
53 if (closed_cb_) {
54 // Move the callback out of |closed_cb_| to prevent it from deleting itself
55 // by deleting |this|.
56 auto f = std::move(closed_cb_);
57 f();
58 }
59 }
60
bondable_mode() const61 sm::BondableMode LowEnergyConnectionHandle::bondable_mode() const {
62 BT_ASSERT(active_);
63 return bondable_cb_();
64 }
65
security() const66 sm::SecurityProperties LowEnergyConnectionHandle::security() const {
67 BT_ASSERT(active_);
68 return security_cb_();
69 }
70
71 } // namespace bt::gap
72