Skip to main content

driver_manager_config/
driver_manager_config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_drivermanagerconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0x43, 0x72, 0x3a, 0xe4, 0xb7, 0x68, 0xac, 0xb9, 0xc7, 0xd1, 0x5c, 0xba, 0x35, 0xe4, 0x05, 0x0b,
8    0xaa, 0x1f, 0x63, 0x10, 0x8f, 0xa2, 0x5e, 0xe3, 0x75, 0xbe, 0x52, 0x93, 0x07, 0xc3, 0xe5, 0x84,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub delay_fallback_until_base_drivers_indexed: bool,
14    pub enable_test_shutdown_delays: bool,
15    pub power_inject_offer: bool,
16    pub power_suspend_enabled: bool,
17    pub root_driver: String,
18    pub set_root_driver_host_critical: bool,
19    pub suspend_timeout_fallback: 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            delay_fallback_until_base_drivers_indexed: fidl_config
61                .delay_fallback_until_base_drivers_indexed,
62            enable_test_shutdown_delays: fidl_config.enable_test_shutdown_delays,
63            power_inject_offer: fidl_config.power_inject_offer,
64            power_suspend_enabled: fidl_config.power_suspend_enabled,
65            root_driver: fidl_config.root_driver,
66            set_root_driver_host_critical: fidl_config.set_root_driver_host_critical,
67            suspend_timeout_fallback: fidl_config.suspend_timeout_fallback,
68            wait_for_suspending_token: fidl_config.wait_for_suspending_token,
69        })
70    }
71    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
72        let fidl_config = FidlConfig {
73            delay_fallback_until_base_drivers_indexed: self
74                .delay_fallback_until_base_drivers_indexed
75                .clone(),
76            enable_test_shutdown_delays: self.enable_test_shutdown_delays.clone(),
77            power_inject_offer: self.power_inject_offer.clone(),
78            power_suspend_enabled: self.power_suspend_enabled.clone(),
79            root_driver: self.root_driver.clone(),
80            set_root_driver_host_critical: self.set_root_driver_host_critical.clone(),
81            suspend_timeout_fallback: self.suspend_timeout_fallback.clone(),
82            wait_for_suspending_token: self.wait_for_suspending_token.clone(),
83        };
84        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
85        let mut bytes = Vec::with_capacity(
86            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
87        );
88        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
89        bytes.extend_from_slice(EXPECTED_CHECKSUM);
90        bytes.append(&mut fidl_bytes);
91        Ok(bytes)
92    }
93    fn record_inspect(&self, inspector_node: &Node) {
94        inspector_node.record_bool(
95            "delay_fallback_until_base_drivers_indexed",
96            self.delay_fallback_until_base_drivers_indexed,
97        );
98        inspector_node.record_bool("enable_test_shutdown_delays", self.enable_test_shutdown_delays);
99        inspector_node.record_bool("power_inject_offer", self.power_inject_offer);
100        inspector_node.record_bool("power_suspend_enabled", self.power_suspend_enabled);
101        inspector_node.record_string("root_driver", &self.root_driver);
102        inspector_node
103            .record_bool("set_root_driver_host_critical", self.set_root_driver_host_critical);
104        inspector_node.record_bool("suspend_timeout_fallback", self.suspend_timeout_fallback);
105        inspector_node.record_bool("wait_for_suspending_token", self.wait_for_suspending_token);
106    }
107}