virtcon_config/
virtcon_config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_virtconconfig::Config as FidlConfig;
3use fuchsia_inspect::{ArithmeticArrayProperty, Node};
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5#[derive(Debug)]
6pub struct Config {
7    pub boot_animation: bool,
8    pub buffer_count: u32,
9    pub color_scheme: String,
10    pub disable: bool,
11    pub display_rotation: u32,
12    pub dpi: Vec<u32>,
13    pub font_size: String,
14    pub keep_log_visible: bool,
15    pub key_map: String,
16    pub keyrepeat: bool,
17    pub rounded_corners: bool,
18    pub scrollback_rows: u32,
19}
20impl Config {
21    pub fn take_from_startup_handle() -> Self {
22        let config_vmo: zx::Vmo =
23            take_startup_handle(HandleInfo::new(HandleType::ComponentConfigVmo, 0))
24                .expect("Config VMO handle must be provided and cannot already have been taken.")
25                .into();
26        let config_size =
27            config_vmo.get_content_size().expect("must be able to read config vmo content size");
28        assert_ne!(config_size, 0, "config vmo must be non-empty");
29        let config_bytes =
30            config_vmo.read_to_vec(0, config_size).expect("must be able to read config vmo");
31        let checksum_length = u16::from_le_bytes([config_bytes[0], config_bytes[1]]) as usize;
32        let fidl_start = 2 + checksum_length;
33        let observed_checksum = &config_bytes[2..fidl_start];
34        let expected_checksum = vec![
35            0x74, 0x6e, 0xae, 0x39, 0xee, 0xe2, 0xab, 0xf5, 0x23, 0xae, 0x2e, 0xfd, 0x27, 0x31,
36            0x55, 0x10, 0x56, 0xd5, 0xd8, 0xc2, 0x51, 0x64, 0x42, 0x84, 0x68, 0xd1, 0xea, 0x75,
37            0xf6, 0xaf, 0x96, 0xd7,
38        ];
39        assert_eq!(
40            observed_checksum, expected_checksum,
41            "checksum from config VMO does not match expected checksum"
42        );
43        let fidl_config: FidlConfig = unpersist(&config_bytes[fidl_start..])
44            .expect("must be able to parse bytes as config FIDL");
45        Self {
46            boot_animation: fidl_config.boot_animation,
47            buffer_count: fidl_config.buffer_count,
48            color_scheme: fidl_config.color_scheme,
49            disable: fidl_config.disable,
50            display_rotation: fidl_config.display_rotation,
51            dpi: fidl_config.dpi,
52            font_size: fidl_config.font_size,
53            keep_log_visible: fidl_config.keep_log_visible,
54            key_map: fidl_config.key_map,
55            keyrepeat: fidl_config.keyrepeat,
56            rounded_corners: fidl_config.rounded_corners,
57            scrollback_rows: fidl_config.scrollback_rows,
58        }
59    }
60    pub fn record_inspect(&self, inspector_node: &Node) {
61        inspector_node.record_bool("boot_animation", self.boot_animation);
62        inspector_node.record_uint("buffer_count", self.buffer_count as u64);
63        inspector_node.record_string("color_scheme", &self.color_scheme);
64        inspector_node.record_bool("disable", self.disable);
65        inspector_node.record_uint("display_rotation", self.display_rotation as u64);
66        let arr = inspector_node.create_uint_array("dpi", self.dpi.len());
67        for i in 0..self.dpi.len() {
68            arr.add(i, self.dpi[i] as u64);
69        }
70        inspector_node.record(arr);
71        inspector_node.record_string("font_size", &self.font_size);
72        inspector_node.record_bool("keep_log_visible", self.keep_log_visible);
73        inspector_node.record_string("key_map", &self.key_map);
74        inspector_node.record_bool("keyrepeat", self.keyrepeat);
75        inspector_node.record_bool("rounded_corners", self.rounded_corners);
76        inspector_node.record_uint("scrollback_rows", self.scrollback_rows as u64);
77    }
78}