// Copyright 2024 The Pigweed Authors // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy of // the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations under // the License. #pragma once #include #include #include #include "fidl/fuchsia.bluetooth.host/cpp/fidl.h" #include "fidl/fuchsia.hardware.bluetooth/cpp/fidl.h" #include "pw_bluetooth_sapphire/internal/host/gap/adapter.h" #include "pw_bluetooth_sapphire/internal/host/gatt/gatt.h" #include "pw_random_fuchsia/zircon_random_generator.h" namespace bthost { class HostServer; class BtHostComponent { public: // Creates a new Host. static std::unique_ptr Create( async_dispatcher_t* dispatcher, const std::string& device_path); // Does not override RNG static std::unique_ptr CreateForTesting( async_dispatcher_t* dispatcher, const std::string& device_path); ~BtHostComponent(); // Initializes the system and reports the status to the |init_cb| in // |success|. |error_cb| will be called if a transport error occurs in the // Host after initialization. Returns false if initialization fails, otherwise // returns true. using InitCallback = fit::callback; using ErrorCallback = fit::callback; [[nodiscard]] bool Initialize( fidl::ClientEnd vendor_client_end, InitCallback init_cb, ErrorCallback error_cb, bool legacy_pairing_enabled); // Shuts down all systems. void ShutDown(); // Binds the given |host_client| to a Host FIDL interface server. void BindToHostInterface( fidl::ServerEnd host_client); std::string device_path() { return device_path_; } using WeakPtr = WeakSelf::WeakPtr; WeakPtr GetWeakPtr() { return weak_self_.GetWeakPtr(); } private: BtHostComponent(async_dispatcher_t* dispatcher, const std::string& device_path, bool initialize_rng); pw::async_fuchsia::FuchsiaDispatcher pw_dispatcher_; // Path of bt-hci device the component supports std::string device_path_; bool initialize_rng_; pw::random_fuchsia::ZirconRandomGenerator random_generator_; std::unique_ptr hci_; std::unique_ptr gap_; // The GATT profile layer and bus. std::unique_ptr gatt_; // Currently connected Host interface handle. // A Host allows only one of these to be connected at a time. std::unique_ptr host_server_; // Inspector for component inspect tree. This object is thread-safe. inspect::ComponentInspector inspector_; WeakSelf weak_self_{this}; BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(BtHostComponent); }; } // namespace bthost