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 <fidl/fuchsia.driver.framework/cpp/fidl.h> 18 #include <fidl/fuchsia.hardware.bluetooth/cpp/fidl.h> 19 #include <lib/driver/component/cpp/driver_base.h> 20 #include <lib/driver/devfs/cpp/connector.h> 21 22 #include "pw_bluetooth_sapphire/fuchsia/bt_hci_virtual/emulator.h" 23 #include "pw_bluetooth_sapphire/fuchsia/bt_hci_virtual/loopback.h" 24 25 namespace bt_hci_virtual { 26 27 // The VirtualController class implements the 28 // fuchsia.hardware.bluetooth.VirtualController API. It is used to created two 29 // devices: the EmulatorDevice and/or the LoopbackDevice. The EmulatorDevice is 30 // used for Bluetooth integration tests, and the LoopbackDevice is used by 31 // RootCanal for PTS-bot. 32 // 33 // VirtualController publishes itself as a DFv2 driver and starts a device node 34 // to bind to said driver. It can create a EmulatorDevice/LoopbackDevice which 35 // can then use the VirtualController's child node 36 // |virtual_controller_child_node_| to publish its own children node that would 37 // represent the EmulatorDevice/LoopbackDevice. 38 // 39 // EmulatorDevice/LoopbackDevice then implements and serves the FIDL protocols 40 // that their client needs. For more details, refer to 41 // go/bluetooth-virtual-driver-doc. 42 class VirtualController 43 : public fdf::DriverBase, 44 public fidl::WireAsyncEventHandler< 45 fuchsia_driver_framework::NodeController>, 46 public fidl::WireAsyncEventHandler<fuchsia_driver_framework::Node>, 47 public fidl::WireServer<fuchsia_hardware_bluetooth::VirtualController> { 48 public: 49 explicit VirtualController( 50 fdf::DriverStartArgs start_args, 51 fdf::UnownedSynchronizedDispatcher driver_dispatcher); 52 53 // fdf::DriverBase overrides: 54 zx::result<> Start() override; 55 handle_unknown_event(fidl::UnknownEventMetadata<fuchsia_driver_framework::Node> metadata)56 void handle_unknown_event( 57 fidl::UnknownEventMetadata<fuchsia_driver_framework::Node> metadata) 58 override {} handle_unknown_event(fidl::UnknownEventMetadata<fuchsia_driver_framework::NodeController> metadata)59 void handle_unknown_event( 60 fidl::UnknownEventMetadata<fuchsia_driver_framework::NodeController> 61 metadata) override {} 62 63 private: 64 // fuchsia_hardware_bluetooth::VirtualController overrides: 65 void CreateEmulator(CreateEmulatorCompleter::Sync& completer) override; 66 void CreateLoopbackDevice( 67 CreateLoopbackDeviceRequestView request, 68 CreateLoopbackDeviceCompleter::Sync& completer) override; 69 void handle_unknown_method( 70 fidl::UnknownMethodMetadata<fuchsia_hardware_bluetooth::VirtualController> 71 metadata, 72 fidl::UnknownMethodCompleter::Sync& completer) override; 73 74 void Connect( 75 fidl::ServerEnd<fuchsia_hardware_bluetooth::VirtualController> request); 76 77 // Helpers functions to add DFv2 device nodes 78 zx_status_t AddVirtualControllerChildNode( 79 fuchsia_driver_framework::wire::NodeAddArgs args); 80 zx_status_t AddLoopbackChildNode( 81 fuchsia_driver_framework::wire::NodeAddArgs args); 82 zx_status_t AddEmulatorChildNode( 83 fuchsia_driver_framework::wire::NodeAddArgs args, 84 EmulatorDevice* emulator_device); 85 86 std::unique_ptr<EmulatorDevice> emulator_device_; 87 std::unique_ptr<LoopbackDevice> loopback_device_; 88 89 // VirtualController 90 fidl::WireClient<fuchsia_driver_framework::Node> node_; 91 fidl::WireClient<fuchsia_driver_framework::NodeController> node_controller_; 92 fidl::WireClient<fuchsia_driver_framework::Node> 93 virtual_controller_child_node_; 94 driver_devfs::Connector<fuchsia_hardware_bluetooth::VirtualController> 95 devfs_connector_; 96 fidl::ServerBindingGroup<fuchsia_hardware_bluetooth::VirtualController> 97 virtual_controller_binding_group_; 98 99 // LoopbackDevice 100 fidl::WireClient<fuchsia_driver_framework::NodeController> 101 loopback_node_controller_; 102 fidl::WireClient<fuchsia_driver_framework::Node> loopback_child_node_; 103 104 // EmulatorDevice 105 fidl::WireClient<fuchsia_driver_framework::NodeController> 106 emulator_node_controller_; 107 fidl::WireClient<fuchsia_driver_framework::Node> emulator_child_node_; 108 }; 109 110 } // namespace bt_hci_virtual 111