omaha_client_structured_config/
omaha-client-structured-config-lib_rust_config_lib_source.rs
1use fidl::unpersist;
2use fidl_cf_sc_internal_omahaclientstructuredconfig::Config as FidlConfig;
3use fuchsia_inspect::Node;
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5#[derive(Debug)]
6pub struct Config {
7 pub allow_reboot_when_idle: bool,
8 pub fuzz_percentage_range: u8,
9 pub periodic_interval_minutes: u16,
10 pub retry_delay_seconds: u16,
11 pub startup_delay_seconds: u16,
12}
13impl Config {
14 pub fn take_from_startup_handle() -> Self {
15 let config_vmo: zx::Vmo =
16 take_startup_handle(HandleInfo::new(HandleType::ComponentConfigVmo, 0))
17 .expect("Config VMO handle must be provided and cannot already have been taken.")
18 .into();
19 let config_size =
20 config_vmo.get_content_size().expect("must be able to read config vmo content size");
21 assert_ne!(config_size, 0, "config vmo must be non-empty");
22 let config_bytes =
23 config_vmo.read_to_vec(0, config_size).expect("must be able to read config vmo");
24 let checksum_length = u16::from_le_bytes([config_bytes[0], config_bytes[1]]) as usize;
25 let fidl_start = 2 + checksum_length;
26 let observed_checksum = &config_bytes[2..fidl_start];
27 let expected_checksum = vec![
28 0x70, 0x33, 0xd6, 0x73, 0x02, 0x0a, 0x40, 0x60, 0x94, 0xfa, 0x2b, 0x27, 0x15, 0x17,
29 0x86, 0x3c, 0x72, 0x5b, 0x0c, 0xd1, 0x8d, 0x78, 0xcc, 0xec, 0xce, 0x1b, 0xe8, 0x41,
30 0xed, 0x16, 0x58, 0xd4,
31 ];
32 assert_eq!(
33 observed_checksum, expected_checksum,
34 "checksum from config VMO does not match expected checksum"
35 );
36 let fidl_config: FidlConfig = unpersist(&config_bytes[fidl_start..])
37 .expect("must be able to parse bytes as config FIDL");
38 Self {
39 allow_reboot_when_idle: fidl_config.allow_reboot_when_idle,
40 fuzz_percentage_range: fidl_config.fuzz_percentage_range,
41 periodic_interval_minutes: fidl_config.periodic_interval_minutes,
42 retry_delay_seconds: fidl_config.retry_delay_seconds,
43 startup_delay_seconds: fidl_config.startup_delay_seconds,
44 }
45 }
46 pub fn record_inspect(&self, inspector_node: &Node) {
47 inspector_node.record_bool("allow_reboot_when_idle", self.allow_reboot_when_idle);
48 inspector_node.record_uint("fuzz_percentage_range", self.fuzz_percentage_range as u64);
49 inspector_node
50 .record_uint("periodic_interval_minutes", self.periodic_interval_minutes as u64);
51 inspector_node.record_uint("retry_delay_seconds", self.retry_delay_seconds as u64);
52 inspector_node.record_uint("startup_delay_seconds", self.startup_delay_seconds as u64);
53 }
54}