教程:在驱动程序之间传递元数据

本指南介绍了如何使用元数据库将元数据从一个驱动程序传递到另一个驱动程序。元数据是驱动程序绑定到节点时创建的任何任意数据,该节点应可供绑定到其子节点的驱动程序访问。通常,驱动程序可以创建自己的 FIDL 协议来传递元数据,不过,元数据库提供的功能可以使用更少的代码在驱动程序之间传递元数据。

您可以在此处找到司机发送、检索和转发元数据的示例。

元数据定义

首先,必须将元数据类型定义为 FIDL 类型:

library fuchsia.examples.metadata;

// Type of the metadata to be passed.
type Metadata = table {
    1: test_property string:MAX;
};

这些元数据将使用 fuchsia.driver.metadata/Service FIDL 服务在驱动程序的传出命名空间中提供。不过,传出命名空间中的服务名称不会是 fuchsia.driver.metadata.Service。而是我们提供的自定义字符串,该字符串与元数据类型相关联。在本教程中,我们将在与上文相同的 FIDL 库中定义该字符串:

library fuchsia.examples.metadata;

const SERVICE string = "fuchsia.examples.metadata.Metadata";

FIDL 库的 build 目标定义如下:

fidl("fuchsia.examples.metadata") {
  sources = [ "fuchsia.examples.metadata.fidl" ]
}

元数据库

为了使用元数据库同时发送和接收元数据,我们需要对 fdf_metadata::ObjectDetails 模板类进行专门化处理,如下所示:

// Defines `fdf_metadata::ObjectDetails`.
#include <lib/driver/metadata/cpp/metadata.h>

// Defines the fuchsia.examples.metadata types in C++.
#include <fidl/fuchsia.examples.metadata/cpp/fidl.h>

// `ObjectDetails` must be specialized within the `fdf_metadata` namespace.
namespace fdf_metadata {

template <>
// Pass the metadata type as the template argument.
struct ObjectDetails<fuchsia_examples_metadata::Metadata> {

  // Specify the name of the service used to serve the metadata.
  inline static const char* Name = fuchsia_examples_metadata::kService;
};

}  // namespace fdf_metadata

由于发送端驱动程序和接收端驱动程序都需要执行此操作,因此我们可以将此代码插入将由这两个驱动程序包含的新库中:

source_set("examples_metadata") {
  # This header file should contain the above code.
  sources = [ "examples_metadata.h" ]

  public_deps = [
    # This should be the fuchsia.examples.metadata FIDL library's C++ target.
    ":fuchsia.examples.metadata_cpp",
  ]
}

发送元数据

初始设置

假设我们有一个要向其子项发送元数据的驱动程序:

#include <lib/driver/component/cpp/driver_export.h>

class Sender : public fdf::DriverBase {
 public:
  Sender(fdf::DriverStartArgs start_args, fdf::UnownedSynchronizedDispatcher driver_dispatcher)
      : DriverBase("parent", std::move(start_args), std::move(driver_dispatcher)) {}
};

FUCHSIA_DRIVER_EXPORT(Sender);

其组件清单如下所示:

{
    include: [
        "inspect/client.shard.cml",
        "syslog/client.shard.cml",
    ],
    program: {
        runner: "driver",
        binary: "driver/parent.so",
        bind: "meta/bind/parent.bindbc",
    },
}

其 build 目标定义如下:

fuchsia_cc_driver("driver") {
  testonly = true
  output_name = "parent"
  sources = [
    "parent.cc",
  ]
  deps = [
    "//src/devices/lib/driver:driver_runtime",
  ]
}

fuchsia_driver_component("component") {
  testonly = true
  component_name = "parent"
  manifest = "meta/parent.cml"
  deps = [
    ":driver",
    ":bind", # Bind rules not specified in this tutorial.
  ]
  info = "parent.json" # Info not specified in this tutorial.
}

发送流程

为了让此驱动程序向其子驱动程序发送元数据,它需要 fdf_metadata::MetadataServer 类的实例,通过调用其 fdf_metadata::MetadataServer::SetMetadata() 方法设置元数据,然后通过调用其 fdf_metadata::MetadataServer::Serve() 方法将元数据提供给驱动程序的传出目录:


// Make sure to include the metadata library that specializes the
// `fdf_metadata::ObjectDetails` class. It is needed by
// `fdf_metadata::MetadataServer`.
#include "examples_metadata.h"

#include <lib/driver/metadata/cpp/metadata_server.h>

class Sender : public fdf::DriverBase {
 public:
  zx::result<> Start() override {
    // Set the metadata to be served.
    fuchsia_examples_metadata::Metadata metadata{{.test_property = "test value"}};
    ZX_ASSERT(metadata_server_.SetMetadata(std::move(metadata)) == ZX_OK);

    // Serve the metadata to the driver's outgoing directory.
    ZX_ASSERT(metadata_server_.Serve(*outgoing(), dispatcher()) == ZX_OK);

    return zx::ok();
  }

 private:
  // Responsible for serving metadata.
  fdf_metadata::MetadataServer<fuchsia_examples_metadata::Metadata> metadata_server_;
};

您可以在 fdf_metadata::MetadataServer::Serve() 之前或之后多次调用 fdf_metadata::MetadataServer::SetMetadata(),元数据服务器将提供最新的元数据实例。如果驱动程序在尝试检索元数据之前尚未调用 fdf_metadata::MetadataServer::SetMetadata(),将无法检索元数据。

需要更新 driver 构建目标:fuchsia_cc_driver("driver") { testonly = true output_name = "parent" sources = [ "parent.cc", ] deps = [ "//src/devices/lib/driver:driver_runtime", ":examples_metadata", ] }

公开元数据服务

最后,驱动程序需要在其组件清单中声明并公开 fuchsia.examples.metadata.Metadata FIDL 服务:

{
    include: [
        "inspect/client.shard.cml",
        "syslog/client.shard.cml",
    ],
    program: {
        runner: "driver",
        binary: "driver/parent.so",
        bind: "meta/bind/parent.bindbc",
    },
    capabilities: [
        { service: "fuchsia.examples.metadata.Metadata" },
    ],
    expose: [
        {
            service: "fuchsia.examples.metadata.Metadata",
            from: "self",
        },
    ],
}

从技术层面讲,没有 fuchsia.examples.metadata.Metadata FIDL 服务。在底层,fdf_metadata::MetadataServer 会提供 fuchsia.driver.metadata/Service FIDL 服务,以便发送元数据。不过,fdf_metadata::MetadataServer 不会像普通 FIDL 服务那样以 fuchsia.driver.metadata.Service 的名称提供此服务。而是以 fuchsia.examples.metadata.Metadata 的名称提供服务。这样,接收驱动程序就可以确定正在传递的元数据类型。这意味着,即使该服务不存在,我们也需要声明并公开 fuchsia.examples.metadata.Metadata 服务。

检索元数据

初始设置

假设我们有一个驱动程序想要从其父级驱动程序接收元数据:

#include <lib/driver/component/cpp/driver_export.h>

class Retriever : public fdf::DriverBase {
 public:
  Retriever(fdf::DriverStartArgs start_args, fdf::UnownedSynchronizedDispatcher driver_dispatcher)
      : DriverBase("child", std::move(start_args), std::move(driver_dispatcher)) {}
};

FUCHSIA_DRIVER_EXPORT(Retriever);

其组件清单如下所示:

{
    include: [
        "inspect/client.shard.cml",
        "syslog/client.shard.cml",
    ],
    program: {
        runner: "driver",
        binary: "driver/child.so",
        bind: "meta/bind/child.bindbc",
    },
}

其 build 目标定义如下:

fuchsia_cc_driver("driver") {
  testonly = true
  output_name = "child"
  sources = [
    "child.cc",
  ]
  deps = [
    "//src/devices/lib/driver:driver_runtime",
  ]
}

fuchsia_driver_component("component") {
  testonly = true
  component_name = "child"
  manifest = "meta/child.cml"
  deps = [
    ":driver",
    ":bind", # Bind rules not specified in this tutorial.
  ]
  info = "child.json" # Info not specified in this tutorial.
}

检索过程

为了从驱动程序的父级驱动程序检索元数据,驱动程序将调用 fdf_metadata::GetMetadata()

#include <lib/driver/metadata/cpp/metadata.h>

// Make sure to include the metadata library that specializes the
// `fdf_metadata::ObjectDetails` class. It is needed by
// `fdf_metadata::GetMetadata()`.
#include "examples_metadata.h"

class Retriever : public fdf::DriverBase {
 public:
  zx::result<> Start() override {
    zx::result<fuchsia_examples_metadata::Metadata> metadata =
      fdf_metadata::GetMetadata<fuchsia_examples_metadata::Metadata>(incoming());
    ZX_ASSERT(!metadata.is_error());

    return zx::ok();
  }
};

需要更新 driver build 目标:

fuchsia_cc_driver("driver") {
  testonly = true
  output_name = "child"
  sources = [
    "child.cc",
  ]
  deps = [
    "//src/devices/lib/driver:driver_runtime",
    ":examples_metadata",
  ]
}

使用元数据服务

最后,驱动程序需要在其组件清单中声明对 fuchsia.examples.metadata.Metadata FIDL 服务的使用:

{
    include: [
        "inspect/client.shard.cml",
        "syslog/client.shard.cml",
    ],
    program: {
        runner: "driver",
        binary: "driver/child.so",
        bind: "meta/bind/child.bindbc",
    },
    use: [
        { service: "fuchsia.examples.metadata.Metadata" },
    ],
}

转发元数据

初始设置

假设我们有一个驱动程序想要从其父级驱动程序检索元数据,并将这些元数据转发给其子级驱动程序:

#include <lib/driver/component/cpp/driver_export.h>

class Forwarder : public fdf::DriverBase {
 public:
  Forwarder(fdf::DriverStartArgs start_args, fdf::UnownedSynchronizedDispatcher driver_dispatcher)
      : DriverBase("forward", std::move(start_args), std::move(driver_dispatcher)) {}
};

FUCHSIA_DRIVER_EXPORT(Forwarder);

其组件清单如下所示:

{
    include: [
        "inspect/client.shard.cml",
        "syslog/client.shard.cml",
    ],
    program: {
        runner: "driver",
        binary: "driver/forward_driver.so",
        bind: "meta/bind/forward.bindbc",
    },
}

它的构建目标定义如下:

fuchsia_cc_driver("driver") {
  testonly = true
  output_name = "forward_driver"
  sources = [
    "forward_driver.cc",
  ]
  deps = [
    "//src/devices/lib/driver:driver_runtime",
  ]
}

fuchsia_driver_component("component") {
  testonly = true
  component_name = "forward"
  manifest = "meta/forward.cml"
  deps = [
    ":driver",
    ":bind", # Bind rules not specified in this tutorial.
  ]
  info = "forward.json" # Info not specified in this tutorial.
}

正向处理

为了让驱动程序将元数据从其父级驱动程序转发到其子级驱动程序,它需要 fdf_metadata::MetadataServer 类的实例,通过调用其 fdf_metadata::MetadataServer::ForwardMetadata() 方法设置元数据,然后通过调用其 fdf_metadata::MetadataServer::Serve() 方法将元数据提供给驱动程序的传出目录:

// Make sure to include the metadata library that specializes the
// `fdf_metadata::ObjectDetails` class. It is needed by
// `fdf_metadata::MetadataServer`.
#include "examples_metadata.h"

#include <lib/driver/metadata/cpp/metadata_server.h>

class Forwarder : public fdf::DriverBase {
 public:
  zx::result<> Start() override {
    // Set metadata using the driver's parent driver metadata.
    ZX_ASSERT(metadata_server_.ForwardMetadata(incoming()) == ZX_OK);

    // Serve the metadata to the driver's outgoing directory.
    ZX_ASSERT(metadata_server_.Serve(*outgoing(), dispatcher()) == ZX_OK);

    return zx::ok();
  }

 private:
  // Responsible for serving metadata.
  fdf_metadata::MetadataServer<fuchsia_examples_metadata::Metadata> metadata_server_;
};

请注意,在调用 fdf_metadata::MetadataServer::ForwardMetadata() 后,fdf_metadata::MetadataServer::ForwardMetadata() 不会检查父驱动程序是否更改了它提供的元数据。驱动程序必须再次调用 fdf_metadata::MetadataServer::ForwardMetadata() 才能纳入更改。

需要更新 driver build 目标:

fuchsia_cc_driver("driver") {
  testonly = true
  output_name = "forward_driver"
  sources = [
    "forward_driver.cc",
  ]
  deps = [
    "//src/devices/lib/driver:driver_runtime",
    ":examples_metadata",
  ]
}

公开和使用元数据服务

最后,驱动程序将需要在其组件清单中声明、使用和公开 fuchsia.examples.metadata.Metadata FIDL 服务:

{
    include: [
        "inspect/client.shard.cml",
        "syslog/client.shard.cml",
    ],
    program: {
        runner: "driver",
        binary: "driver/forward_driver.so",
        bind: "meta/bind/forward.bindbc",
    },
    capabilities: [
        { service: "fuchsia.examples.metadata.Metadata" },
    ],
    expose: [
        {
            service: "fuchsia.examples.metadata.Metadata",
            from: "self",
        },
    ],
    use: [
        { service: "fuchsia.examples.metadata.Metadata" },
    ],
}