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 <lib/async/dispatcher.h> 18 #include <lib/inspect/component/cpp/component.h> 19 #include <pw_async_fuchsia/dispatcher.h> 20 21 #include "fidl/fuchsia.bluetooth.host/cpp/fidl.h" 22 #include "fidl/fuchsia.hardware.bluetooth/cpp/fidl.h" 23 #include "pw_bluetooth_sapphire/internal/host/gap/adapter.h" 24 #include "pw_bluetooth_sapphire/internal/host/gatt/gatt.h" 25 #include "pw_random_fuchsia/zircon_random_generator.h" 26 27 namespace bthost { 28 29 class HostServer; 30 31 class BtHostComponent { 32 public: 33 // Creates a new Host. 34 static std::unique_ptr<BtHostComponent> Create( 35 async_dispatcher_t* dispatcher, const std::string& device_path); 36 37 // Does not override RNG 38 static std::unique_ptr<BtHostComponent> CreateForTesting( 39 async_dispatcher_t* dispatcher, const std::string& device_path); 40 41 ~BtHostComponent(); 42 43 // Initializes the system and reports the status to the |init_cb| in 44 // |success|. |error_cb| will be called if a transport error occurs in the 45 // Host after initialization. Returns false if initialization fails, otherwise 46 // returns true. 47 using InitCallback = fit::callback<void(bool success)>; 48 using ErrorCallback = fit::callback<void()>; 49 [[nodiscard]] bool Initialize( 50 fidl::ClientEnd<fuchsia_hardware_bluetooth::Vendor> vendor_client_end, 51 InitCallback init_cb, 52 ErrorCallback error_cb, 53 bool legacy_pairing_enabled); 54 55 // Shuts down all systems. 56 void ShutDown(); 57 58 // Binds the given |host_client| to a Host FIDL interface server. 59 void BindToHostInterface( 60 fidl::ServerEnd<fuchsia_bluetooth_host::Host> host_client); 61 device_path()62 std::string device_path() { return device_path_; } 63 64 using WeakPtr = WeakSelf<BtHostComponent>::WeakPtr; GetWeakPtr()65 WeakPtr GetWeakPtr() { return weak_self_.GetWeakPtr(); } 66 67 private: 68 BtHostComponent(async_dispatcher_t* dispatcher, 69 const std::string& device_path, 70 bool initialize_rng); 71 72 pw::async_fuchsia::FuchsiaDispatcher pw_dispatcher_; 73 74 // Path of bt-hci device the component supports 75 std::string device_path_; 76 77 bool initialize_rng_; 78 79 pw::random_fuchsia::ZirconRandomGenerator random_generator_; 80 81 std::unique_ptr<bt::hci::Transport> hci_; 82 83 std::unique_ptr<bt::gap::Adapter> gap_; 84 85 // The GATT profile layer and bus. 86 std::unique_ptr<bt::gatt::GATT> gatt_; 87 88 // Currently connected Host interface handle. 89 // A Host allows only one of these to be connected at a time. 90 std::unique_ptr<HostServer> host_server_; 91 92 // Inspector for component inspect tree. This object is thread-safe. 93 inspect::ComponentInspector inspector_; 94 95 WeakSelf<BtHostComponent> weak_self_{this}; 96 97 BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(BtHostComponent); 98 }; 99 100 } // namespace bthost 101