Google is committed to advancing racial equity for Black communities. See how.

Using FIDL proxies with plugins

FFX plugins can communicate with a target device using FIDL through Overnet.

The examples in this doc extend the example code from the plugins page.

First we add a dependency to the plugin's BUILD.gn file:

import("//src/developer/ffx/build/ffx_plugin.gni")

ffx_plugin("ffx_example") {
  version = "0.1.0"
  edition = "2018"
  with_unit_tests = true
  deps = [
    "//sdk/fidl/fuchsia.device:fuchsia.device-rustc",
  ]
  sources = [
    "src/args.rs",
    "src/lib.rs",
  ]
}

This makes the FIDL proxy bindings available for usage in the plugin. In this case you can now import NameProviderProxy.

use {
    anyhow::Result,
    ffx_core::ffx_plugin,
    ffx_example_args::ExampleCommand,
    fidl_fuchsia_device::NameProviderProxy,
};

Now that the type is imported, the proxy can be used in the plugin function. FFX plugins can accept proxies in the parameter list:

pub async fn example(
    name_proxy: NameProviderProxy,
    _cmd: ExampleCommand,
) -> Result<()> { }

In order to correctly connect a proxy to the FIDL service on the target, you will need to map the proxy type to a component selector that can be used to find the FIDL service. More about component selectors can be found on the Component Select page. This mapping is passed into the ffx_plugin annotation at the top of the function signature:

#[ffx_plugin(
    NameProviderProxy = "core/appmgr:out:fuchsia.device.NameProvider"
)]

Putting it all together, your src/lib.rs file should look like:

use {
    anyhow::Result,
    ffx_core::ffx_plugin,
    ffx_example_args::ExampleCommand,
    fidl_fuchsia_device::NameProviderProxy,
};

#[ffx_plugin(
    NameProviderProxy = "core/appmgr:out:fuchsia.device.NameProvider"
)]
pub async fn example(
    name_proxy: NameProviderProxy,
    _cmd: ExampleCommand,
) -> Result<()> {
    if let Ok(name) = name_proxy.get_device_name().await? {
        println!("Hello, {}", name);
    }
    Ok(())
}

And that's it. The plugin should now be able to communicate with target device using native FIDL calls. FFX plugins can accept any number of plugins as long as the same steps are followed and the proxies are correctly mapped to component selectors.

There are two exceptions to this rule. FFX already knows how to communicate with two proxies without mappings. It's enough to just add these proxies to the parameter list without changing the ffx_plugin annotation: