hwinfo_structured_config/
hwinfo_structured_config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_hwinfostructuredconfig::Config as FidlConfig;
3use fuchsia_inspect::Node;
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5#[derive(Debug)]
6pub struct Config {
7    pub product_manufacturer: String,
8    pub product_model: String,
9    pub product_name: 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            0xa4, 0x87, 0x1c, 0x22, 0x13, 0xd9, 0x41, 0xa7, 0xe1, 0xad, 0xa4, 0xf0, 0xc3, 0xb2,
27            0xdf, 0xbb, 0x24, 0x49, 0x70, 0x36, 0x35, 0xf0, 0x46, 0xeb, 0x1a, 0x5b, 0x0f, 0xaf,
28            0x13, 0x4c, 0x83, 0xcc,
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            product_manufacturer: fidl_config.product_manufacturer,
38            product_model: fidl_config.product_model,
39            product_name: fidl_config.product_name,
40        }
41    }
42    pub fn record_inspect(&self, inspector_node: &Node) {
43        inspector_node.record_string("product_manufacturer", &self.product_manufacturer);
44        inspector_node.record_string("product_model", &self.product_model);
45        inspector_node.record_string("product_name", &self.product_name);
46    }
47}