timekeeper_config/
timekeeper-config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_timekeeperconfig::Config as FidlConfig;
3use fuchsia_inspect::Node;
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0x44, 0x44, 0x26, 0xa2, 0x6d, 0xf1, 0x47, 0xf4, 0x3e, 0x48, 0xdb, 0x89, 0xf9, 0x6f, 0x39, 0xff,
8    0x73, 0x07, 0xae, 0x5f, 0xac, 0x84, 0x26, 0xba, 0x77, 0xc0, 0x7f, 0xfc, 0x14, 0xc7, 0xd2, 0xa7,
9];
10#[derive(Debug)]
11pub struct Config {
12    pub back_off_time_between_pull_samples_sec: i64,
13    pub disable_delays: bool,
14    pub early_exit: bool,
15    pub first_sampling_delay_sec: i64,
16    pub has_always_on_counter: bool,
17    pub has_real_time_clock: bool,
18    pub initial_frequency_ppm: u32,
19    pub max_frequency_error_ppm: u32,
20    pub monitor_time_source_url: String,
21    pub monitor_uses_pull: bool,
22    pub oscillator_error_std_dev_ppm: u32,
23    pub power_topology_integration_enabled: bool,
24    pub primary_time_source_url: String,
25    pub primary_uses_pull: bool,
26    pub serve_fuchsia_time_alarms: bool,
27    pub serve_fuchsia_time_external_adjust: bool,
28    pub serve_test_protocols: bool,
29    pub use_connectivity: bool,
30    pub utc_max_allowed_delta_future_sec: i64,
31    pub utc_max_allowed_delta_past_sec: i64,
32    pub utc_start_at_startup: bool,
33    pub utc_start_at_startup_when_invalid_rtc: bool,
34}
35impl Config {
36    #[doc = r" Take the config startup handle and parse its contents."]
37    #[doc = r""]
38    #[doc = r" # Panics"]
39    #[doc = r""]
40    #[doc = r" If the config startup handle was already taken or if it is not valid."]
41    pub fn take_from_startup_handle() -> Self {
42        let handle_info = HandleInfo::new(HandleType::ComponentConfigVmo, 0);
43        let config_vmo: zx::Vmo =
44            take_startup_handle(handle_info).expect("Config VMO handle must be present.").into();
45        Self::from_vmo(&config_vmo).expect("Config VMO handle must be valid.")
46    }
47    #[doc = r" Parse `Self` from `vmo`."]
48    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
49        let config_size = vmo.get_content_size().map_err(Error::GettingContentSize)?;
50        let config_bytes = vmo.read_to_vec(0, config_size).map_err(Error::ReadingConfigBytes)?;
51        Self::from_bytes(&config_bytes)
52    }
53    #[doc = r" Parse `Self` from `bytes`."]
54    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
55        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
56        let checksum_len_bytes: [u8; 2] =
57            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
58        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
59        let (observed_checksum, bytes) =
60            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
61        if observed_checksum != EXPECTED_CHECKSUM {
62            return Err(Error::ChecksumMismatch { observed_checksum: observed_checksum.to_vec() });
63        }
64        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
65        Ok(Self {
66            back_off_time_between_pull_samples_sec: fidl_config
67                .back_off_time_between_pull_samples_sec,
68            disable_delays: fidl_config.disable_delays,
69            early_exit: fidl_config.early_exit,
70            first_sampling_delay_sec: fidl_config.first_sampling_delay_sec,
71            has_always_on_counter: fidl_config.has_always_on_counter,
72            has_real_time_clock: fidl_config.has_real_time_clock,
73            initial_frequency_ppm: fidl_config.initial_frequency_ppm,
74            max_frequency_error_ppm: fidl_config.max_frequency_error_ppm,
75            monitor_time_source_url: fidl_config.monitor_time_source_url,
76            monitor_uses_pull: fidl_config.monitor_uses_pull,
77            oscillator_error_std_dev_ppm: fidl_config.oscillator_error_std_dev_ppm,
78            power_topology_integration_enabled: fidl_config.power_topology_integration_enabled,
79            primary_time_source_url: fidl_config.primary_time_source_url,
80            primary_uses_pull: fidl_config.primary_uses_pull,
81            serve_fuchsia_time_alarms: fidl_config.serve_fuchsia_time_alarms,
82            serve_fuchsia_time_external_adjust: fidl_config.serve_fuchsia_time_external_adjust,
83            serve_test_protocols: fidl_config.serve_test_protocols,
84            use_connectivity: fidl_config.use_connectivity,
85            utc_max_allowed_delta_future_sec: fidl_config.utc_max_allowed_delta_future_sec,
86            utc_max_allowed_delta_past_sec: fidl_config.utc_max_allowed_delta_past_sec,
87            utc_start_at_startup: fidl_config.utc_start_at_startup,
88            utc_start_at_startup_when_invalid_rtc: fidl_config
89                .utc_start_at_startup_when_invalid_rtc,
90        })
91    }
92    pub fn record_inspect(&self, inspector_node: &Node) {
93        inspector_node.record_int(
94            "back_off_time_between_pull_samples_sec",
95            self.back_off_time_between_pull_samples_sec,
96        );
97        inspector_node.record_bool("disable_delays", self.disable_delays);
98        inspector_node.record_bool("early_exit", self.early_exit);
99        inspector_node.record_int("first_sampling_delay_sec", self.first_sampling_delay_sec);
100        inspector_node.record_bool("has_always_on_counter", self.has_always_on_counter);
101        inspector_node.record_bool("has_real_time_clock", self.has_real_time_clock);
102        inspector_node.record_uint("initial_frequency_ppm", self.initial_frequency_ppm as u64);
103        inspector_node.record_uint("max_frequency_error_ppm", self.max_frequency_error_ppm as u64);
104        inspector_node.record_string("monitor_time_source_url", &self.monitor_time_source_url);
105        inspector_node.record_bool("monitor_uses_pull", self.monitor_uses_pull);
106        inspector_node
107            .record_uint("oscillator_error_std_dev_ppm", self.oscillator_error_std_dev_ppm as u64);
108        inspector_node.record_bool(
109            "power_topology_integration_enabled",
110            self.power_topology_integration_enabled,
111        );
112        inspector_node.record_string("primary_time_source_url", &self.primary_time_source_url);
113        inspector_node.record_bool("primary_uses_pull", self.primary_uses_pull);
114        inspector_node.record_bool("serve_fuchsia_time_alarms", self.serve_fuchsia_time_alarms);
115        inspector_node.record_bool(
116            "serve_fuchsia_time_external_adjust",
117            self.serve_fuchsia_time_external_adjust,
118        );
119        inspector_node.record_bool("serve_test_protocols", self.serve_test_protocols);
120        inspector_node.record_bool("use_connectivity", self.use_connectivity);
121        inspector_node
122            .record_int("utc_max_allowed_delta_future_sec", self.utc_max_allowed_delta_future_sec);
123        inspector_node
124            .record_int("utc_max_allowed_delta_past_sec", self.utc_max_allowed_delta_past_sec);
125        inspector_node.record_bool("utc_start_at_startup", self.utc_start_at_startup);
126        inspector_node.record_bool(
127            "utc_start_at_startup_when_invalid_rtc",
128            self.utc_start_at_startup_when_invalid_rtc,
129        );
130    }
131}
132#[derive(Debug)]
133pub enum Error {
134    #[doc = r" Failed to read the content size of the VMO."]
135    GettingContentSize(zx::Status),
136    #[doc = r" Failed to read the content of the VMO."]
137    ReadingConfigBytes(zx::Status),
138    #[doc = r" The VMO was too small for this config library."]
139    TooFewBytes,
140    #[doc = r" The VMO's config ABI checksum did not match this library's."]
141    ChecksumMismatch { observed_checksum: Vec<u8> },
142    #[doc = r" Failed to parse the non-checksum bytes of the VMO as this library's FIDL type."]
143    Unpersist(fidl::Error),
144}
145impl std::fmt::Display for Error {
146    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147        match self {
148            Self::GettingContentSize(status) => {
149                write!(f, "Failed to get content size: {status}")
150            }
151            Self::ReadingConfigBytes(status) => {
152                write!(f, "Failed to read VMO content: {status}")
153            }
154            Self::TooFewBytes => {
155                write!(f, "VMO content is not large enough for this config library.")
156            }
157            Self::ChecksumMismatch { observed_checksum } => {
158                write!(
159                    f,
160                    "ABI checksum mismatch, expected {:?}, got {:?}",
161                    EXPECTED_CHECKSUM, observed_checksum,
162                )
163            }
164            Self::Unpersist(fidl_error) => {
165                write!(f, "Failed to parse contents of config VMO: {fidl_error}")
166            }
167        }
168    }
169}
170impl std::error::Error for Error {
171    #[allow(unused_parens, reason = "rustfmt errors without parens here")]
172    fn source(&self) -> Option<(&'_ (dyn std::error::Error + 'static))> {
173        match self {
174            Self::GettingContentSize(ref status) | Self::ReadingConfigBytes(ref status) => {
175                Some(status)
176            }
177            Self::TooFewBytes => None,
178            Self::ChecksumMismatch { .. } => None,
179            Self::Unpersist(ref fidl_error) => Some(fidl_error),
180        }
181    }
182    fn description(&self) -> &str {
183        match self {
184            Self::GettingContentSize(_) => "getting content size",
185            Self::ReadingConfigBytes(_) => "reading VMO contents",
186            Self::TooFewBytes => "VMO contents too small",
187            Self::ChecksumMismatch { .. } => "ABI checksum mismatch",
188            Self::Unpersist(_) => "FIDL parsing error",
189        }
190    }
191}