1use fidl::unpersist;
2use fidl_cf_sc_internal_httpsdateconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7 0x3f, 0x42, 0x60, 0x4a, 0xaf, 0xd0, 0x56, 0x01, 0xd6, 0x31, 0x3c, 0x8e, 0x87, 0x68, 0x05, 0x3c,
8 0x5b, 0x8e, 0x08, 0x56, 0xef, 0x08, 0xe4, 0x7a, 0xa8, 0x90, 0xd3, 0xa4, 0x75, 0xfd, 0xd6, 0xaa,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13 pub first_rtt_time_factor: u16,
14 pub https_timeout_sec: u8,
15 pub is_monitor_time_source: bool,
16 pub max_attempts_urgency_high: u32,
17 pub max_attempts_urgency_low: u32,
18 pub max_attempts_urgency_medium: u32,
19 pub num_polls_urgency_high: u32,
20 pub num_polls_urgency_low: u32,
21 pub num_polls_urgency_medium: u32,
22 pub standard_deviation_bound_percentage: u8,
23 pub time_source_endpoint_url: String,
24 pub use_fake_sampler: bool,
25 pub use_pull_api: bool,
26}
27impl Config {
28 #[doc = r" Take the config startup handle and parse its contents."]
29 #[doc = r""]
30 #[doc = r" # Panics"]
31 #[doc = r""]
32 #[doc = r" If the config startup handle was already taken or if it is not valid."]
33 pub fn take_from_startup_handle() -> Self {
34 <Self as ComponentConfig>::take_from_startup_handle()
35 }
36 #[doc = r" Parse `Self` from `vmo`."]
37 pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
38 <Self as ComponentConfig>::from_vmo(vmo)
39 }
40 #[doc = r" Parse `Self` from `bytes`."]
41 pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
42 <Self as ComponentConfig>::from_bytes(bytes)
43 }
44 pub fn record_inspect(&self, inspector_node: &Node) {
45 <Self as ComponentConfig>::record_inspect(self, inspector_node)
46 }
47}
48impl ComponentConfig for Config {
49 #[doc = r" Parse `Self` from `bytes`."]
50 fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
51 let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
52 let checksum_len_bytes: [u8; 2] =
53 checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
54 let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
55 let (observed_checksum, bytes) =
56 bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
57 if observed_checksum != EXPECTED_CHECKSUM {
58 return Err(Error::ChecksumMismatch {
59 expected_checksum: EXPECTED_CHECKSUM.to_vec(),
60 observed_checksum: observed_checksum.to_vec(),
61 });
62 }
63 let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
64 Ok(Self {
65 first_rtt_time_factor: fidl_config.first_rtt_time_factor,
66 https_timeout_sec: fidl_config.https_timeout_sec,
67 is_monitor_time_source: fidl_config.is_monitor_time_source,
68 max_attempts_urgency_high: fidl_config.max_attempts_urgency_high,
69 max_attempts_urgency_low: fidl_config.max_attempts_urgency_low,
70 max_attempts_urgency_medium: fidl_config.max_attempts_urgency_medium,
71 num_polls_urgency_high: fidl_config.num_polls_urgency_high,
72 num_polls_urgency_low: fidl_config.num_polls_urgency_low,
73 num_polls_urgency_medium: fidl_config.num_polls_urgency_medium,
74 standard_deviation_bound_percentage: fidl_config.standard_deviation_bound_percentage,
75 time_source_endpoint_url: fidl_config.time_source_endpoint_url,
76 use_fake_sampler: fidl_config.use_fake_sampler,
77 use_pull_api: fidl_config.use_pull_api,
78 })
79 }
80 fn to_bytes(&self) -> Result<Vec<u8>, Error> {
81 let fidl_config = FidlConfig {
82 first_rtt_time_factor: self.first_rtt_time_factor.clone(),
83 https_timeout_sec: self.https_timeout_sec.clone(),
84 is_monitor_time_source: self.is_monitor_time_source.clone(),
85 max_attempts_urgency_high: self.max_attempts_urgency_high.clone(),
86 max_attempts_urgency_low: self.max_attempts_urgency_low.clone(),
87 max_attempts_urgency_medium: self.max_attempts_urgency_medium.clone(),
88 num_polls_urgency_high: self.num_polls_urgency_high.clone(),
89 num_polls_urgency_low: self.num_polls_urgency_low.clone(),
90 num_polls_urgency_medium: self.num_polls_urgency_medium.clone(),
91 standard_deviation_bound_percentage: self.standard_deviation_bound_percentage.clone(),
92 time_source_endpoint_url: self.time_source_endpoint_url.clone(),
93 use_fake_sampler: self.use_fake_sampler.clone(),
94 use_pull_api: self.use_pull_api.clone(),
95 };
96 let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
97 let mut bytes = Vec::with_capacity(
98 EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
99 );
100 bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
101 bytes.extend_from_slice(EXPECTED_CHECKSUM);
102 bytes.append(&mut fidl_bytes);
103 Ok(bytes)
104 }
105 fn record_inspect(&self, inspector_node: &Node) {
106 inspector_node.record_uint("first_rtt_time_factor", self.first_rtt_time_factor as u64);
107 inspector_node.record_uint("https_timeout_sec", self.https_timeout_sec as u64);
108 inspector_node.record_bool("is_monitor_time_source", self.is_monitor_time_source);
109 inspector_node
110 .record_uint("max_attempts_urgency_high", self.max_attempts_urgency_high as u64);
111 inspector_node
112 .record_uint("max_attempts_urgency_low", self.max_attempts_urgency_low as u64);
113 inspector_node
114 .record_uint("max_attempts_urgency_medium", self.max_attempts_urgency_medium as u64);
115 inspector_node.record_uint("num_polls_urgency_high", self.num_polls_urgency_high as u64);
116 inspector_node.record_uint("num_polls_urgency_low", self.num_polls_urgency_low as u64);
117 inspector_node
118 .record_uint("num_polls_urgency_medium", self.num_polls_urgency_medium as u64);
119 inspector_node.record_uint(
120 "standard_deviation_bound_percentage",
121 self.standard_deviation_bound_percentage as u64,
122 );
123 inspector_node.record_string("time_source_endpoint_url", &self.time_source_endpoint_url);
124 inspector_node.record_bool("use_fake_sampler", self.use_fake_sampler);
125 inspector_node.record_bool("use_pull_api", self.use_pull_api);
126 }
127}