Skip to main content

sag_config/
sag_config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_sagconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0x1d, 0x56, 0x8f, 0x57, 0x5a, 0x20, 0xf4, 0xe7, 0xdb, 0x21, 0x88, 0xa5, 0xaf, 0x63, 0x2a, 0x47,
8    0xa4, 0x21, 0xab, 0x0a, 0xa4, 0x7e, 0x31, 0xd2, 0x2c, 0xa2, 0xd7, 0x01, 0x95, 0xc8, 0x50, 0x58,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub long_wake_lease_timeout: u32,
14    pub max_active_wake_leases_to_log: u32,
15    pub max_suspend_events_to_log: u32,
16    pub reboot_on_stalled_suspend_blocker: bool,
17    pub suspend_loop_max_attempts: u32,
18    pub suspend_resume_stuck_warning_timeout: u32,
19    pub use_suspender: bool,
20    pub wait_for_suspending_token: bool,
21}
22impl Config {
23    #[doc = r" Take the config startup handle and parse its contents."]
24    #[doc = r""]
25    #[doc = r" # Panics"]
26    #[doc = r""]
27    #[doc = r" If the config startup handle was already taken or if it is not valid."]
28    pub fn take_from_startup_handle() -> Self {
29        <Self as ComponentConfig>::take_from_startup_handle()
30    }
31    #[doc = r" Parse `Self` from `vmo`."]
32    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
33        <Self as ComponentConfig>::from_vmo(vmo)
34    }
35    #[doc = r" Parse `Self` from `bytes`."]
36    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
37        <Self as ComponentConfig>::from_bytes(bytes)
38    }
39    pub fn record_inspect(&self, inspector_node: &Node) {
40        <Self as ComponentConfig>::record_inspect(self, inspector_node)
41    }
42}
43impl ComponentConfig for Config {
44    #[doc = r" Parse `Self` from `bytes`."]
45    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
46        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
47        let checksum_len_bytes: [u8; 2] =
48            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
49        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
50        let (observed_checksum, bytes) =
51            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
52        if observed_checksum != EXPECTED_CHECKSUM {
53            return Err(Error::ChecksumMismatch {
54                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
55                observed_checksum: observed_checksum.to_vec(),
56            });
57        }
58        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
59        Ok(Self {
60            long_wake_lease_timeout: fidl_config.long_wake_lease_timeout,
61            max_active_wake_leases_to_log: fidl_config.max_active_wake_leases_to_log,
62            max_suspend_events_to_log: fidl_config.max_suspend_events_to_log,
63            reboot_on_stalled_suspend_blocker: fidl_config.reboot_on_stalled_suspend_blocker,
64            suspend_loop_max_attempts: fidl_config.suspend_loop_max_attempts,
65            suspend_resume_stuck_warning_timeout: fidl_config.suspend_resume_stuck_warning_timeout,
66            use_suspender: fidl_config.use_suspender,
67            wait_for_suspending_token: fidl_config.wait_for_suspending_token,
68        })
69    }
70    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
71        let fidl_config = FidlConfig {
72            long_wake_lease_timeout: self.long_wake_lease_timeout.clone(),
73            max_active_wake_leases_to_log: self.max_active_wake_leases_to_log.clone(),
74            max_suspend_events_to_log: self.max_suspend_events_to_log.clone(),
75            reboot_on_stalled_suspend_blocker: self.reboot_on_stalled_suspend_blocker.clone(),
76            suspend_loop_max_attempts: self.suspend_loop_max_attempts.clone(),
77            suspend_resume_stuck_warning_timeout: self.suspend_resume_stuck_warning_timeout.clone(),
78            use_suspender: self.use_suspender.clone(),
79            wait_for_suspending_token: self.wait_for_suspending_token.clone(),
80        };
81        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
82        let mut bytes = Vec::with_capacity(
83            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
84        );
85        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
86        bytes.extend_from_slice(EXPECTED_CHECKSUM);
87        bytes.append(&mut fidl_bytes);
88        Ok(bytes)
89    }
90    fn record_inspect(&self, inspector_node: &Node) {
91        inspector_node.record_uint("long_wake_lease_timeout", self.long_wake_lease_timeout as u64);
92        inspector_node.record_uint(
93            "max_active_wake_leases_to_log",
94            self.max_active_wake_leases_to_log as u64,
95        );
96        inspector_node
97            .record_uint("max_suspend_events_to_log", self.max_suspend_events_to_log as u64);
98        inspector_node.record_bool(
99            "reboot_on_stalled_suspend_blocker",
100            self.reboot_on_stalled_suspend_blocker,
101        );
102        inspector_node
103            .record_uint("suspend_loop_max_attempts", self.suspend_loop_max_attempts as u64);
104        inspector_node.record_uint(
105            "suspend_resume_stuck_warning_timeout",
106            self.suspend_resume_stuck_warning_timeout as u64,
107        );
108        inspector_node.record_bool("use_suspender", self.use_suspender);
109        inspector_node.record_bool("wait_for_suspending_token", self.wait_for_suspending_token);
110    }
111}