| // Copyright 2022 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef FUCHSIA_SDK_EXAMPLES_CC_DRIVER_COMPAT_H_ |
| #define FUCHSIA_SDK_EXAMPLES_CC_DRIVER_COMPAT_H_ |
| |
| #include <fidl/fuchsia.device.fs/cpp/wire.h> |
| #include <fidl/fuchsia.driver.compat/cpp/wire.h> |
| |
| namespace input_driver_compat { |
| |
| // Connect to the fuchsia.devices.fs.Exporter protocol |
| zx::status<fidl::ClientEnd<fuchsia_device_fs::Exporter>> ConnectToDeviceExporter( |
| const driver::Namespace* ns) { |
| auto exporter = ns->Connect<fuchsia_device_fs::Exporter>(); |
| if (exporter.is_error()) { |
| return exporter.take_error(); |
| } |
| return exporter; |
| } |
| |
| // Create an exported directory handle using fuchsia.devices.fs.Exporter |
| zx::status<fidl::ServerEnd<fuchsia_io::Directory>> ExportDevfsEntry(const driver::Namespace* ns, |
| fidl::StringView service_dir, |
| fidl::StringView devfs_path, |
| int protocol_id) { |
| // Connect to the devfs exporter service |
| auto exporter_client = ConnectToDeviceExporter(ns); |
| if (exporter_client.is_error()) { |
| return exporter_client.take_error(); |
| } |
| auto exporter = fidl::WireSyncClient(std::move(exporter_client.value())); |
| |
| // Serve a connection for devfs clients |
| auto endpoints = fidl::CreateEndpoints<fuchsia_io::Directory>(); |
| if (endpoints.is_error()) { |
| return endpoints.take_error(); |
| } |
| |
| // Export the client side of the service connection to devfs |
| auto result = |
| exporter->Export(std::move(endpoints->client), service_dir, devfs_path, protocol_id); |
| if (!result.ok()) { |
| return zx::error(result.status()); |
| } |
| if (result->is_error()) { |
| return zx::error(result->error_value()); |
| } |
| |
| return zx::ok(std::move(endpoints->server)); |
| } |
| |
| } // namespace input_driver_compat |
| |
| #endif // FUCHSIA_SDK_EXAMPLES_CC_DRIVER_COMPAT_H_ |