power_manager_config_lib/
power-manager-config-lib_rust_config_lib_source.rsuse fidl::unpersist;
use fidl_cf_sc_internal_powermanagerconfiglib::Config as FidlConfig;
use fuchsia_inspect::Node;
use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
#[derive(Debug)]
pub struct Config {
pub disable_temperature_filter: bool,
pub enable_debug_service: bool,
pub node_config_path: String,
}
impl Config {
pub fn take_from_startup_handle() -> Self {
let config_vmo: zx::Vmo =
take_startup_handle(HandleInfo::new(HandleType::ComponentConfigVmo, 0))
.expect("Config VMO handle must be provided and cannot already have been taken.")
.into();
let config_size =
config_vmo.get_content_size().expect("must be able to read config vmo content size");
assert_ne!(config_size, 0, "config vmo must be non-empty");
let config_bytes =
config_vmo.read_to_vec(0, config_size).expect("must be able to read config vmo");
let checksum_length = u16::from_le_bytes([config_bytes[0], config_bytes[1]]) as usize;
let fidl_start = 2 + checksum_length;
let observed_checksum = &config_bytes[2..fidl_start];
let expected_checksum = vec![
0x65, 0xe6, 0x1e, 0xa0, 0x5f, 0xd2, 0xf5, 0xf9, 0x67, 0x58, 0xac, 0xcd, 0xc1, 0x85,
0x28, 0x7b, 0x13, 0x00, 0xaa, 0x9f, 0x12, 0x1d, 0xfd, 0xc0, 0x8a, 0x7e, 0x90, 0xc9,
0x59, 0x3a, 0x46, 0x9f,
];
assert_eq!(
observed_checksum, expected_checksum,
"checksum from config VMO does not match expected checksum"
);
let fidl_config: FidlConfig = unpersist(&config_bytes[fidl_start..])
.expect("must be able to parse bytes as config FIDL");
Self {
disable_temperature_filter: fidl_config.disable_temperature_filter,
enable_debug_service: fidl_config.enable_debug_service,
node_config_path: fidl_config.node_config_path,
}
}
pub fn record_inspect(&self, inspector_node: &Node) {
inspector_node.record_bool("disable_temperature_filter", self.disable_temperature_filter);
inspector_node.record_bool("enable_debug_service", self.enable_debug_service);
inspector_node.record_string("node_config_path", &self.node_config_path);
}
}