config/
config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_config::Config as FidlConfig;
3use fuchsia_inspect::Node;
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5#[derive(Debug)]
6pub struct Config {
7    pub addend: u8,
8    pub augend: u8,
9    pub do_in_process: bool,
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            0x9c, 0xfa, 0x70, 0x19, 0xd1, 0xc7, 0xa8, 0x95, 0xd5, 0x43, 0x86, 0x0f, 0xae, 0xb7,
27            0x28, 0x0d, 0xb2, 0x19, 0x0f, 0x77, 0x9c, 0xb1, 0x59, 0x9e, 0x1a, 0x08, 0xeb, 0x27,
28            0xfe, 0x7a, 0x21, 0xae,
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            addend: fidl_config.addend,
38            augend: fidl_config.augend,
39            do_in_process: fidl_config.do_in_process,
40        }
41    }
42    pub fn record_inspect(&self, inspector_node: &Node) {
43        inspector_node.record_uint("addend", self.addend as u64);
44        inspector_node.record_uint("augend", self.augend as u64);
45        inspector_node.record_bool("do_in_process", self.do_in_process);
46    }
47}