config_lib/
config_lib_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_configlib::Config as FidlConfig;
3use fuchsia_inspect::{ArrayProperty, Node};
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5#[derive(Debug)]
6pub struct Config {
7    pub echo_bool: bool,
8    pub echo_num: u64,
9    pub echo_string: String,
10    pub echo_string_vector: Vec<String>,
11}
12impl Config {
13    pub fn take_from_startup_handle() -> Self {
14        let config_vmo: zx::Vmo =
15            take_startup_handle(HandleInfo::new(HandleType::ComponentConfigVmo, 0))
16                .expect("Config VMO handle must be provided and cannot already have been taken.")
17                .into();
18        let config_size =
19            config_vmo.get_content_size().expect("must be able to read config vmo content size");
20        assert_ne!(config_size, 0, "config vmo must be non-empty");
21        let config_bytes =
22            config_vmo.read_to_vec(0, config_size).expect("must be able to read config vmo");
23        let checksum_length = u16::from_le_bytes([config_bytes[0], config_bytes[1]]) as usize;
24        let fidl_start = 2 + checksum_length;
25        let observed_checksum = &config_bytes[2..fidl_start];
26        let expected_checksum = vec![
27            0x1e, 0x2c, 0x8c, 0x11, 0x78, 0x21, 0x1b, 0xf4, 0x53, 0xba, 0x24, 0x7c, 0x85, 0xfa,
28            0x38, 0x87, 0xf2, 0xcc, 0x60, 0x5d, 0xdb, 0xba, 0x3d, 0xe6, 0x07, 0x44, 0xa1, 0x0e,
29            0x17, 0xa6, 0x12, 0x97,
30        ];
31        assert_eq!(
32            observed_checksum, expected_checksum,
33            "checksum from config VMO does not match expected checksum"
34        );
35        let fidl_config: FidlConfig = unpersist(&config_bytes[fidl_start..])
36            .expect("must be able to parse bytes as config FIDL");
37        Self {
38            echo_bool: fidl_config.echo_bool,
39            echo_num: fidl_config.echo_num,
40            echo_string: fidl_config.echo_string,
41            echo_string_vector: fidl_config.echo_string_vector,
42        }
43    }
44    pub fn record_inspect(&self, inspector_node: &Node) {
45        inspector_node.record_bool("echo_bool", self.echo_bool);
46        inspector_node.record_uint("echo_num", self.echo_num);
47        inspector_node.record_string("echo_string", &self.echo_string);
48        let arr =
49            inspector_node.create_string_array("echo_string_vector", self.echo_string_vector.len());
50        for i in 0..self.echo_string_vector.len() {
51            arr.set(i, &self.echo_string_vector[i]);
52        }
53        inspector_node.record(arr);
54    }
55}