power_manager_config_lib/
power-manager-config-lib_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_powermanagerconfiglib::Config as FidlConfig;
3use fuchsia_inspect::Node;
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5#[derive(Debug)]
6pub struct Config {
7    pub disable_temperature_filter: bool,
8    pub enable_debug_service: bool,
9    pub node_config_path: String,
10}
11impl Config {
12    pub fn take_from_startup_handle() -> Self {
13        let config_vmo: zx::Vmo =
14            take_startup_handle(HandleInfo::new(HandleType::ComponentConfigVmo, 0))
15                .expect("Config VMO handle must be provided and cannot already have been taken.")
16                .into();
17        let config_size =
18            config_vmo.get_content_size().expect("must be able to read config vmo content size");
19        assert_ne!(config_size, 0, "config vmo must be non-empty");
20        let config_bytes =
21            config_vmo.read_to_vec(0, config_size).expect("must be able to read config vmo");
22        let checksum_length = u16::from_le_bytes([config_bytes[0], config_bytes[1]]) as usize;
23        let fidl_start = 2 + checksum_length;
24        let observed_checksum = &config_bytes[2..fidl_start];
25        let expected_checksum = vec![
26            0x65, 0xe6, 0x1e, 0xa0, 0x5f, 0xd2, 0xf5, 0xf9, 0x67, 0x58, 0xac, 0xcd, 0xc1, 0x85,
27            0x28, 0x7b, 0x13, 0x00, 0xaa, 0x9f, 0x12, 0x1d, 0xfd, 0xc0, 0x8a, 0x7e, 0x90, 0xc9,
28            0x59, 0x3a, 0x46, 0x9f,
29        ];
30        assert_eq!(
31            observed_checksum, expected_checksum,
32            "checksum from config VMO does not match expected checksum"
33        );
34        let fidl_config: FidlConfig = unpersist(&config_bytes[fidl_start..])
35            .expect("must be able to parse bytes as config FIDL");
36        Self {
37            disable_temperature_filter: fidl_config.disable_temperature_filter,
38            enable_debug_service: fidl_config.enable_debug_service,
39            node_config_path: fidl_config.node_config_path,
40        }
41    }
42    pub fn record_inspect(&self, inspector_node: &Node) {
43        inspector_node.record_bool("disable_temperature_filter", self.disable_temperature_filter);
44        inspector_node.record_bool("enable_debug_service", self.enable_debug_service);
45        inspector_node.record_string("node_config_path", &self.node_config_path);
46    }
47}