Drivers can use component framework's structured configuration to read values provided at product assembly time (see providing configuration). See the example C++ and Rust drivers.
Include the configuration use in CML
In your driver CML, use the config capability with the name of your configuration:
use: [
{
config: "fuchsia.power.SuspendEnabled",
key: "suspend_enabled",
type: "bool",
},
],
Set up the build
In the build file, you need to declare the manifest target manually, so that you can use the build target to generate the library to access the configuration:
C++
fuchsia_component_manifest("component-manifest") {
component_name = "example_config_driver"
manifest = "meta/config_driver.cml"
}
fuchsia_structured_config_cpp_elf_lib("example-config-driver-config") {
cm_label = ":component-manifest"
}
fuchsia_driver_component("component") {
cm_label = ":component-manifest"
deps = [
":bind",
":driver",
]
info = "meta/component-info.json"
}
fuchsia_cc_driver("driver") {
output_name = "example_config_driver"
sources = [ "config_driver.cc" ]
deps = [
":example-config-driver-config",
"//sdk/lib/driver/component/cpp",
"//src/devices/lib/driver:driver_runtime",
]
}
Rust
fuchsia_component_manifest("manifest") {
component_name = "example_config_driver_rust"
manifest = "meta/config_driver.cml"
}
fuchsia_structured_config_rust_lib("example-config-driver-config") {
cm_label = ":manifest"
}
fuchsia_driver_component("component") {
cm_label = ":manifest"
deps = [
":bind",
":driver",
]
info = "meta/component-info.json"
}
fuchsia_rust_driver("driver") {
edition = "2024"
output_name = "example_config_driver_rust"
sources = [ "src/lib.rs" ]
deps = [
":example-config-driver-config",
"//sdk/fidl/fuchsia.driver.framework:fuchsia.driver.framework_rust",
"//sdk/lib/driver/component/rust",
"//sdk/lib/driver/runtime/rust",
"//sdk/rust/zx",
"//third_party/rust_crates:log",
]
}
Access the configuration in code
You can now access the configuration values in the code by using the generated library.
Include the library dependency:
C++
#include "examples/drivers/config/cpp/example_config_driver_config.h"Rust
use example_config_driver_config::Config;Use the library to access the configuration from the start arguments of the driver:
C++
auto config = take_config<example_config_driver_config::Config>(); fdf::info("My config value is: {}", config.suspend_enabled());Rust
let config = context.take_config::<Config>()?; info!("My config value is: {}", config.suspend_enabled);