Skip to main content

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_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0x5f, 0xb4, 0x48, 0x47, 0xb9, 0xf7, 0xd4, 0xf6, 0xa9, 0x9b, 0xd2, 0x74, 0x25, 0x53, 0x5f, 0xfa,
8    0xae, 0x07, 0xab, 0x80, 0x63, 0x23, 0x49, 0xba, 0x72, 0xb8, 0x70, 0xea, 0xca, 0xc0, 0x0c, 0x00,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub allow_reboot_when_idle: bool,
14    pub app_id: String,
15    pub fuzz_percentage_range: u8,
16    pub omaha_url: String,
17    pub ota_channel: String,
18    pub ota_realm: String,
19    pub periodic_interval_minutes: u16,
20    pub product_id: String,
21    pub retry_delay_seconds: u16,
22    pub startup_delay_seconds: u16,
23}
24impl Config {
25    #[doc = r" Take the config startup handle and parse its contents."]
26    #[doc = r""]
27    #[doc = r" # Panics"]
28    #[doc = r""]
29    #[doc = r" If the config startup handle was already taken or if it is not valid."]
30    pub fn take_from_startup_handle() -> Self {
31        <Self as ComponentConfig>::take_from_startup_handle()
32    }
33    #[doc = r" Parse `Self` from `vmo`."]
34    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
35        <Self as ComponentConfig>::from_vmo(vmo)
36    }
37    #[doc = r" Parse `Self` from `bytes`."]
38    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
39        <Self as ComponentConfig>::from_bytes(bytes)
40    }
41    pub fn record_inspect(&self, inspector_node: &Node) {
42        <Self as ComponentConfig>::record_inspect(self, inspector_node)
43    }
44}
45impl ComponentConfig for Config {
46    #[doc = r" Parse `Self` from `bytes`."]
47    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
48        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
49        let checksum_len_bytes: [u8; 2] =
50            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
51        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
52        let (observed_checksum, bytes) =
53            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
54        if observed_checksum != EXPECTED_CHECKSUM {
55            return Err(Error::ChecksumMismatch {
56                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
57                observed_checksum: observed_checksum.to_vec(),
58            });
59        }
60        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
61        Ok(Self {
62            allow_reboot_when_idle: fidl_config.allow_reboot_when_idle,
63            app_id: fidl_config.app_id,
64            fuzz_percentage_range: fidl_config.fuzz_percentage_range,
65            omaha_url: fidl_config.omaha_url,
66            ota_channel: fidl_config.ota_channel,
67            ota_realm: fidl_config.ota_realm,
68            periodic_interval_minutes: fidl_config.periodic_interval_minutes,
69            product_id: fidl_config.product_id,
70            retry_delay_seconds: fidl_config.retry_delay_seconds,
71            startup_delay_seconds: fidl_config.startup_delay_seconds,
72        })
73    }
74    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
75        let fidl_config = FidlConfig {
76            allow_reboot_when_idle: self.allow_reboot_when_idle.clone(),
77            app_id: self.app_id.clone(),
78            fuzz_percentage_range: self.fuzz_percentage_range.clone(),
79            omaha_url: self.omaha_url.clone(),
80            ota_channel: self.ota_channel.clone(),
81            ota_realm: self.ota_realm.clone(),
82            periodic_interval_minutes: self.periodic_interval_minutes.clone(),
83            product_id: self.product_id.clone(),
84            retry_delay_seconds: self.retry_delay_seconds.clone(),
85            startup_delay_seconds: self.startup_delay_seconds.clone(),
86        };
87        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
88        let mut bytes = Vec::with_capacity(
89            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
90        );
91        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
92        bytes.extend_from_slice(EXPECTED_CHECKSUM);
93        bytes.append(&mut fidl_bytes);
94        Ok(bytes)
95    }
96    fn record_inspect(&self, inspector_node: &Node) {
97        inspector_node.record_bool("allow_reboot_when_idle", self.allow_reboot_when_idle);
98        inspector_node.record_string("app_id", &self.app_id);
99        inspector_node.record_uint("fuzz_percentage_range", self.fuzz_percentage_range as u64);
100        inspector_node.record_string("omaha_url", &self.omaha_url);
101        inspector_node.record_string("ota_channel", &self.ota_channel);
102        inspector_node.record_string("ota_realm", &self.ota_realm);
103        inspector_node
104            .record_uint("periodic_interval_minutes", self.periodic_interval_minutes as u64);
105        inspector_node.record_string("product_id", &self.product_id);
106        inspector_node.record_uint("retry_delay_seconds", self.retry_delay_seconds as u64);
107        inspector_node.record_uint("startup_delay_seconds", self.startup_delay_seconds as u64);
108    }
109}