驅動程式可以使用元件架構的結構化設定,讀取產品組裝時提供的值 (請參閱提供設定)。請參閱 C++ 和 Rust 驅動程式範例。
在 CML 中加入設定用途
在驅動程式庫 CML 中,use以設定名稱 config 能力:
use: [
{
config: "fuchsia.power.SuspendEnabled",
key: "suspend_enabled",
type: "bool",
},
],
設定版本
您需要在建構檔案中手動宣告資訊清單目標,才能使用建構目標產生可存取設定的程式庫:
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",
]
}
荒漠油廠
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",
]
}
在程式碼中存取設定
現在您可以使用產生的程式庫,在程式碼中存取設定值。
加入程式庫依附元件:
C++
#include "examples/drivers/config/cpp/example_config_driver_config.h"荒漠油廠
use example_config_driver_config::Config;使用程式庫從驅動程式庫的啟動引數存取設定:
C++
auto config = take_config<example_config_driver_config::Config>(); fdf::info("My config value is: {}", config.suspend_enabled());荒漠油廠
let config = context.take_config::<Config>()?; info!("My config value is: {}", config.suspend_enabled);