本指南介绍了如何添加从 并在另一个驱动程序中使用。本指南假定您熟悉 以下概念:
FIDL 协议定义
以下代码段将使用此 FIDL 协议:
library fidl.examples.echo;
const MAX_STRING_LENGTH uint64 = 32;
// The discoverable annotation is required, otherwise the protocol bindings
// will not have a name string generated.
@discoverable
protocol Echo {
/// Returns the input.
EchoString(struct {
value string:<MAX_STRING_LENGTH, optional>;
}) -> (struct {
response string:<MAX_STRING_LENGTH, optional>;
});
};
service EchoService {
echo client_end:Echo;
};
父级驱动程序(服务器)
我们在这里大致估算了实现协议的父级驱动程序 将被写入什么内容。虽然未显示,但我们假定此类是 使用 DDKTL
// This class implement the fuchsia.examples.echo/Echo FIDL protocol using the
// new C++ FIDL bindings
class Device : public fidl::WireServer<fidl_examples_echo::Echo> {
// This is the main entry point for the driver.
static zx_status_t Bind(void* ctx, zx_device_t* parent) {
// When creating the device, we initialize it with a dispatcher provided by
// the driver framework. This dispatcher is allows us to schedule
// asynchronous work on the same thread as other drivers. You may opt to
// create your own dispatcher which is serviced on a thread you spawn if you
// desire instead.
auto* dispatcher = fdf::Dispatcher::GetCurrent()->async_dispatcher();
auto device = std::make_unique<Device>(parent, dispatcher);
// We add the FIDL protocol we wish to export to our child to our outgoing
// directory. When a connection is attempted we will bind the server end of
// the channel pair to our server implementation.
zx::result = device->outgoing_.AddService<fidl_examples_echo::EchoService>(
fidl_examples_echo::EchoService::InstanceHandler({
.echo = device->bindings_.CreateHandler(device.get(), dispatcher,
fidl::kIgnoreBindingClosure),
}));
// Utilizing the server end of the endpoint pair we created above, we bind
// it to our outgoing directory.
result = device->outgoing_.Serve(std::move(endpoints->server));
if (result.is_error()) {
zxlogf(ERROR, "Failed to service the outgoing directory");
return result.status_value();
}
// We declare our outgoing protocols here. These will be utilize to
// help the framework populate node properties which can be used for
// binding.
std::array offers = {
fidl_examples_echo::Service::Name,
};
status = device->DdkAdd(ddk::DeviceAddArgs("parent")
// The device must be spawned in a separate
// driver host.
.set_flags(DEVICE_ADD_MUST_ISOLATE)
.set_fidl_service_offers(offers)
// The client side of outgoing directory is
// provided to the framework. This will be
// forwarded to the new driver host that spawns to
// allow the child driver which binds the ability
// to connect to our outgoing FIDL protocols.
.set_outgoing_dir(endpoints->client.TakeChannel()));
if (status == ZX_OK) {
[[maybe_unused]] auto ptr = device.release();
} else {
zxlogf(ERROR, "Failed to add device");
}
return status;
}
private:
// This is the implementation of the only method our FIDL protocol requires.
void EchoString(EchoStringRequestView request, EchoStringCompleter::Sync& completer) override {
completer.Reply(request->value);
}
// This is a helper class which we use to serve the outgoing directory.
component::OutgoingDirectory outgoing_;
// This ensures that the fidl connections don't outlive the device object.
fidl::ServerBindingGroup<fidl_examples_echo::Echo> bindings_;
};
子驱动程序(客户端)
账号绑定
首先要讨论的重要事项是子驱动程序的绑定方式。它可以 是由于任意数量的节点属性而绑定的,但如果您希望基于 仅基于父级导出的 FIDL 协议,则需要绑定库 build 根据 FIDL 库自动为您生成的 (如需了解详情,请参阅生成的绑定库)。
您将在驱动程序的绑定规则中依赖并使用此绑定库:
using fidl.examples.echo;
fidl.examples.echo.Echo == fidl.examples.echo.Echo.ZirconTransport;
ZirconTransport 是父驱动程序用于 向子节点提供 Echo FIDL 协议。
如果需要,您可以添加其他绑定约束条件。请注意, 仅当父驱动程序声明 FIDL 协议所提供的设备 ID。
客户端代码
可以在已成功 绑定到上述父级驱动程序。
zx_status_t CallEcho() {
// The following method allows us to connect to the protocol we desire. This
// works by providing the server end of our endpoint pair to the framework. It
// will push this channel through the outgoing directory to our parent driver
// which will then bind it to its server implementation. We do not need to
// name the protocol because the method is templated on the channel type and
// it is able to automatically derive the name from the type.
zx::result client_end = DdkConnectFidlProtocol<fidl_examples_echo::EchoService::Echo>();
if (client_end.is_error()) {
zxlogf(ERROR, "Failed to connect fidl protocol: %s", client_end.status_string());
return client_end.status_value();
}
// We turn the client side of the endpoint pair into a synchronous client.
fidl::WireSyncClient client{std::move(client_end.value())};
// We can now utilize our client to make calls!
constexpr std::string_view kInput = "Test String";
auto result = client->EchoString(fidl::StringView::FromExternal(cpp17::string_view(kInput)));
if (!result.ok()) {
zxlogf(ERROR, "Failed to call EchoString");
return result.status();
}
if (result->response.get() != kInput) {
zxlogf(ERROR, "Unexpected response: Actual: \"%.*s\", Expected: \"%.*s\"",
static_cast<int>(result->response.size()), result->response.data(),
static_cast<int>(kInput.size()), kInput.data());
return ZX_ERR_INTERNAL;
}
return ZX_OK;
}
生成的绑定库
所有 FIDL 库都会根据它们自动生成一个绑定库。这是为了帮助驾驶员 作者根据父实体提供的 FIDL 协议和服务创建绑定规则, 父项用于提供每个参数的传输方法。
绑定库
这些绑定库中有三种可能的传输方法:Banjo
、ZirconTransport
、
和 DriverTransport
。目前,可以放心地假定值为 ZirconTransport
。
(这只是 Zircon 信道上的常规 FIDL),或DriverTransport
(针对位于同一位置的驱动程序的进程内通信堆栈)。
绑定库包含协议和这些传输方法的常量。
FIDL 库中定义的每项服务和可检测到的协议都会在 将库与枚举的值绑定为三种传输方法。
以下是 FIDL 库包含一个可检测到的协议的示例:
protocol.fidl
library fuchsia.gizmo.protocol;
@discoverable
closed protocol TestingProtocol {
strict Get();
};
生成的库
// WARNING: This file is machine generated by bindc.
library fuchsia.gizmo.protocol;
enum TestingProtocol {
Banjo,
ZirconTransport,
DriverTransport,
};
构建目标
这些生成的绑定库将基于 FIDL 库的
library_name
和target_name
。绑定库的目标名称为
{fidl_target_name}_bindlib
,并且其 library_name
将与 FIDL 相同。
例如,如果 FIDL 库目标是 //sdk/fidl/fidl.examples.echo:myecholibrary
,
那么自动生成的绑定库目标就是
//sdk/fidl/fidl.examples.echo:myecholibrary_bindlib
。
实际上,大多数 FIDL 库与其所在的文件夹具有相同的 target_name
,即
通常也 是库的名称例如,如果 FIDL 库是
//sdk/fidl/fidl.examples.echo
,自动生成的绑定库目标为
//sdk/fidl/fidl.examples.echo:fidl.examples.echo_bindlib
。
生成的代码目标
这些生成的绑定库的工作方式与由用户编写的绑定库完全相同 绑定库。如需详细了解用户编写的绑定库的代码生成功能,请访问 绑定库代码生成教程。
示例
我们以上文所示的 FIDL 库为例,将其作为示例来使用。
FIDL (BUILD.gn)
fidl("my_fidl_target") { # The target_name
name = "fuchsia.gizmo.protocol" # The library_name (optional, defaults to
# target_name)
sources = [ "protocol.fidl" ]
}
现在即可获取目标名称为 :my_fidl_target_bindlib
的生成的绑定库
,库名称为 fuchsia.gizmo.protocol
。已显示为绑定库生成的源代码
之前。我们可以用它来为子驱动程序创建绑定规则。
子绑定规则 (BUILD.gn)
driver_bind_rules("bind") {
rules = "meta/child_driver.bind"
bind_output = "child_driver.bindbc"
deps = [ ":my_fidl_target_bindlib" ]
}
child-driver.bind
using fuchsia.gizmo.protocol;
fuchsia.gizmo.protocol.TestingProtocol == fuchsia.gizmo.protocol.TestingProtocol.ZirconTransport;
当驱动程序创建子节点时,系统会自动为每个子节点分配一个属性
其在 fuchsia_driver_framework::NodeAddArgs
表中的 offers
。因此,家长驾驶员
则不必手动指定此属性
例如,如果为节点提供基于驱动程序传输的服务功能,
fuchsia_hardware_gizmo::Service
时,将使用
fuchsia.hardware.gizmo.Service
和 fuchsia.hardware.gizmo.Service.DriverTransport
的值。
这些值将与子驱动程序生成的相应绑定库变量相匹配
将在其绑定规则中使用。
在创建复合节点规范时,生成的这段代码仍然有用, 这通常发生在板级驱动程序中。规范的属性必须填写 如果规范想要与基于这些节点的节点相匹配, 优惠。
我们可以使用自动生成的代码目标从 复合节点规范创建代码
composite-node-specification Creator (BUILD.gn)
C++
source_set("bindlib_usage_cpp") {
sources = [ "bindlib_usage.cc" ]
deps = [ ":my_fidl_target_bindlib_cpp" ]
}
Rust
rustc_binary("bindlib_usage_rust") {
edition = "2021"
source_root = "bindlib_usage.rs"
sources = [ "bindlib_usage.rs" ]
deps = [ ":my_fidl_target_bindlib_rust" ]
}
composite-node-specification Creator 代码
C++
#include <bind/fuchsia/gizmo/protocol/cpp/bind.h>
std::string test_protocol_key = bind_fuchsia_gizmo_protocol::TESTINGPROTOCOL;
std::string test_protocol_value = bind_fuchsia_gizmo_protocol::TESTINGPROTOCOL_ZIRCONTRANSPORT;
Rust
fn main() {
let _test_protocol_key: &str = bind_fuchsia_gizmo_protocol::TESTINGPROTOCOL;
let _test_protocol_value: &str = bind_fuchsia_gizmo_protocol::TESTINGPROTOCOL_ZIRCONTRANSPORT;
}