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/iso/iso_common.h" 19 #include "pw_bluetooth_sapphire/internal/host/sm/types.h" 20 21 namespace bt::gap { 22 23 namespace internal { 24 class LowEnergyConnection; 25 } 26 27 class LowEnergyConnectionManager; 28 29 class LowEnergyConnectionHandle final { 30 public: 31 using AcceptCisCallback = fit::function<iso::AcceptCisStatus( 32 iso::CigCisIdentifier, iso::CisEstablishedCallback)>; 33 34 // |release_cb| will be called when this handle releases its reference to the 35 // connection. |accept_cis_cb| will be called to allow an incoming Isochronous 36 // stream to be established with the specified CIG/CIS identifier pair. 37 // |bondable_cb| returns the current bondable mode of the connection. It will 38 // only be called while the connection is active. |security_mode| returns the 39 // current security properties of the connection. It will only be called while 40 // the connection is active. 41 LowEnergyConnectionHandle( 42 PeerId peer_id, 43 hci_spec::ConnectionHandle handle, 44 fit::callback<void(LowEnergyConnectionHandle*)> release_cb, 45 AcceptCisCallback accept_cis_cb, 46 fit::function<sm::BondableMode()> bondable_cb, 47 fit::function<sm::SecurityProperties()> security_cb, 48 fit::function<pw::bluetooth::emboss::ConnectionRole()> role_cb); 49 50 // Destroying this object releases its reference to the underlying connection. 51 ~LowEnergyConnectionHandle(); 52 53 // Releases this object's reference to the underlying connection. 54 void Release(); 55 56 // Returns true if the underlying connection is still active. active()57 bool active() const { return active_; } 58 59 // Sets a callback to be called when the underlying connection is closed. set_closed_callback(fit::closure callback)60 void set_closed_callback(fit::closure callback) { 61 closed_cb_ = std::move(callback); 62 } 63 64 // Allow an incoming Isochronous stream for the specified CIG/CIS identifier 65 // pair. Upon receiving the request, invoke |cis_established_cb| with the 66 // status and if successful, connection parameters. 67 [[nodiscard]] iso::AcceptCisStatus AcceptCis( 68 iso::CigCisIdentifier id, iso::CisEstablishedCallback cis_established_cb); 69 70 // Returns the operational bondable mode of the underlying connection. See 71 // spec V5.1 Vol 3 Part C Section 9.4 for more details. 72 sm::BondableMode bondable_mode() const; 73 74 sm::SecurityProperties security() const; 75 76 pw::bluetooth::emboss::ConnectionRole role() const; 77 peer_identifier()78 PeerId peer_identifier() const { return peer_id_; } handle()79 hci_spec::ConnectionHandle handle() const { return handle_; } 80 81 private: 82 friend class LowEnergyConnectionManager; 83 friend class internal::LowEnergyConnection; 84 85 // Called by LowEnergyConnectionManager when the underlying connection is 86 // closed. Notifies |closed_cb_|. 87 void MarkClosed(); 88 89 bool active_; 90 PeerId peer_id_; 91 hci_spec::ConnectionHandle handle_; 92 fit::closure closed_cb_; 93 fit::callback<void(LowEnergyConnectionHandle*)> release_cb_; 94 AcceptCisCallback accept_cis_cb_; 95 fit::function<sm::BondableMode()> bondable_cb_; 96 fit::function<sm::SecurityProperties()> security_cb_; 97 fit::function<pw::bluetooth::emboss::ConnectionRole()> role_cb_; 98 99 BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(LowEnergyConnectionHandle); 100 }; 101 102 } // namespace bt::gap 103