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 #pragma once 16 #include "pw_bluetooth_sapphire/internal/host/common/identifier.h" 17 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h" 18 #include "pw_bluetooth_sapphire/internal/host/sm/types.h" 19 20 namespace bt::gap { 21 22 namespace internal { 23 class LowEnergyConnection; 24 } 25 26 class LowEnergyConnectionManager; 27 28 class LowEnergyConnectionHandle final { 29 public: 30 // |release_cb| will be called when this handle releases its reference to the 31 // connection. |bondable_cb| returns the current bondable mode of the 32 // connection. It will only be called while the connection is active. 33 // |security_mode| returns the current security properties of the connection. 34 // It will only be called while the connection is active. 35 LowEnergyConnectionHandle( 36 PeerId peer_id, 37 hci_spec::ConnectionHandle handle, 38 fit::callback<void(LowEnergyConnectionHandle*)> release_cb, 39 fit::function<sm::BondableMode()> bondable_cb, 40 fit::function<sm::SecurityProperties()> security_cb); 41 42 // Destroying this object releases its reference to the underlying connection. 43 ~LowEnergyConnectionHandle(); 44 45 // Releases this object's reference to the underlying connection. 46 void Release(); 47 48 // Returns true if the underlying connection is still active. active()49 bool active() const { return active_; } 50 51 // Sets a callback to be called when the underlying connection is closed. set_closed_callback(fit::closure callback)52 void set_closed_callback(fit::closure callback) { 53 closed_cb_ = std::move(callback); 54 } 55 56 // Returns the operational bondable mode of the underlying connection. See 57 // spec V5.1 Vol 3 Part C Section 9.4 for more details. 58 sm::BondableMode bondable_mode() const; 59 60 sm::SecurityProperties security() const; 61 peer_identifier()62 PeerId peer_identifier() const { return peer_id_; } handle()63 hci_spec::ConnectionHandle handle() const { return handle_; } 64 65 private: 66 friend class LowEnergyConnectionManager; 67 friend class internal::LowEnergyConnection; 68 69 // Called by LowEnergyConnectionManager when the underlying connection is 70 // closed. Notifies |closed_cb_|. 71 void MarkClosed(); 72 73 bool active_; 74 PeerId peer_id_; 75 hci_spec::ConnectionHandle handle_; 76 fit::closure closed_cb_; 77 fit::callback<void(LowEnergyConnectionHandle*)> release_cb_; 78 fit::function<sm::BondableMode()> bondable_cb_; 79 fit::function<sm::SecurityProperties()> security_cb_; 80 81 BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(LowEnergyConnectionHandle); 82 }; 83 84 } // namespace bt::gap 85