1 // Copyright 2024 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 17 #include <fuchsia/bluetooth/le/cpp/fidl.h> 18 #include <lib/fidl/cpp/interface_request.h> 19 20 #include "pw_bluetooth_sapphire/fuchsia/host/fidl/channel_server.h" 21 #include "pw_bluetooth_sapphire/fuchsia/host/fidl/gatt2_client_server.h" 22 #include "pw_bluetooth_sapphire/fuchsia/host/fidl/iso_stream_server.h" 23 #include "pw_bluetooth_sapphire/fuchsia/host/fidl/server_base.h" 24 #include "pw_bluetooth_sapphire/internal/host/common/macros.h" 25 #include "pw_bluetooth_sapphire/internal/host/gap/low_energy_connection_handle.h" 26 #include "pw_bluetooth_sapphire/internal/host/gatt/gatt.h" 27 #include "pw_bluetooth_sapphire/internal/host/iso/iso_common.h" 28 29 namespace bthost { 30 31 class LowEnergyConnectionServer 32 : public ServerBase<fuchsia::bluetooth::le::Connection> { 33 public: 34 // |closed_cb| will be called to signal the invalidation of this connection 35 // instance. This can be called in response to the client closing its end of 36 // the FIDL channel or when the LL connection is severed. |closed_cb| will be 37 // called at most once in response to either of these events. The owner of the 38 // LowEnergyConnectionServer instance is expected to destroy it. 39 LowEnergyConnectionServer( 40 bt::gap::Adapter::WeakPtr adapter, 41 bt::gatt::GATT::WeakPtr gatt, 42 std::unique_ptr<bt::gap::LowEnergyConnectionHandle> connection, 43 zx::channel handle, 44 fit::callback<void()> closed_cb); 45 46 // Return a reference to the underlying connection ref. Expected to only be 47 // used for testing. conn()48 const bt::gap::LowEnergyConnectionHandle* conn() const { return conn_.get(); } 49 50 private: 51 void OnClosed(); 52 void ServeChannel( 53 bt::l2cap::Channel::WeakPtr channel, 54 fidl::InterfaceRequest<fuchsia::bluetooth::Channel> request); 55 56 // fuchsia::bluetooth::le::Connection overrides: 57 void RequestGattClient( 58 ::fidl::InterfaceRequest<::fuchsia::bluetooth::gatt2::Client> client) 59 override; 60 void AcceptCis( 61 fuchsia::bluetooth::le::ConnectionAcceptCisRequest parameters) override; 62 void GetCodecLocalDelayRange( 63 ::fuchsia::bluetooth::le::CodecDelayGetCodecLocalDelayRangeRequest 64 CodecDelayGetCodecLocalDelayRangeRequest, 65 GetCodecLocalDelayRangeCallback callback) override; 66 void ConnectL2cap( 67 fuchsia::bluetooth::le::ConnectionConnectL2capRequest request) override; 68 69 std::unique_ptr<bt::gap::LowEnergyConnectionHandle> conn_; 70 fit::callback<void()> closed_handler_; 71 bt::PeerId peer_id_; 72 bt::gap::Adapter::WeakPtr adapter_; 73 bt::gatt::GATT::WeakPtr gatt_; 74 std::optional<Gatt2ClientServer> gatt_client_server_; 75 76 std::unordered_map<bt::iso::CigCisIdentifier, 77 std::unique_ptr<IsoStreamServer>> 78 iso_streams_; 79 std::unordered_map<bt::l2cap::Channel::UniqueId, 80 std::unique_ptr<ChannelServer>> 81 channel_servers_; 82 83 WeakSelf<LowEnergyConnectionServer> weak_self_; // Keep last. 84 85 BT_DISALLOW_COPY_ASSIGN_AND_MOVE(LowEnergyConnectionServer); 86 }; 87 88 } // namespace bthost 89