use fidl::unpersist;
use fidl_cf_sc_internal_timekeeperconfig::Config as FidlConfig;
use fuchsia_inspect::Node;
use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
#[derive(Debug)]
pub struct Config {
pub back_off_time_between_pull_samples_sec: i64,
pub disable_delays: bool,
pub early_exit: bool,
pub first_sampling_delay_sec: i64,
pub has_real_time_clock: bool,
pub initial_frequency_ppm: u32,
pub max_frequency_error_ppm: u32,
pub monitor_time_source_url: String,
pub monitor_uses_pull: bool,
pub oscillator_error_std_dev_ppm: u32,
pub power_topology_integration_enabled: bool,
pub primary_time_source_url: String,
pub primary_uses_pull: bool,
pub serve_fuchsia_time_alarms: bool,
pub serve_test_protocols: bool,
pub utc_start_at_startup: bool,
pub utc_start_at_startup_when_invalid_rtc: bool,
}
impl Config {
pub fn take_from_startup_handle() -> Self {
let config_vmo: zx::Vmo =
take_startup_handle(HandleInfo::new(HandleType::ComponentConfigVmo, 0))
.expect("Config VMO handle must be provided and cannot already have been taken.")
.into();
let config_size =
config_vmo.get_content_size().expect("must be able to read config vmo content size");
assert_ne!(config_size, 0, "config vmo must be non-empty");
let config_bytes =
config_vmo.read_to_vec(0, config_size).expect("must be able to read config vmo");
let checksum_length = u16::from_le_bytes([config_bytes[0], config_bytes[1]]) as usize;
let fidl_start = 2 + checksum_length;
let observed_checksum = &config_bytes[2..fidl_start];
let expected_checksum = vec![
0x78, 0x74, 0xb8, 0x4a, 0xc9, 0x0f, 0xe7, 0xba, 0x2a, 0xda, 0x7e, 0x8b, 0x25, 0xd7,
0x78, 0xa1, 0x6a, 0x10, 0x05, 0xc4, 0xaa, 0xd4, 0xeb, 0xc1, 0x81, 0xd7, 0x98, 0xf5,
0x8e, 0xc3, 0x14, 0xa4,
];
assert_eq!(
observed_checksum, expected_checksum,
"checksum from config VMO does not match expected checksum"
);
let fidl_config: FidlConfig = unpersist(&config_bytes[fidl_start..])
.expect("must be able to parse bytes as config FIDL");
Self {
back_off_time_between_pull_samples_sec: fidl_config
.back_off_time_between_pull_samples_sec,
disable_delays: fidl_config.disable_delays,
early_exit: fidl_config.early_exit,
first_sampling_delay_sec: fidl_config.first_sampling_delay_sec,
has_real_time_clock: fidl_config.has_real_time_clock,
initial_frequency_ppm: fidl_config.initial_frequency_ppm,
max_frequency_error_ppm: fidl_config.max_frequency_error_ppm,
monitor_time_source_url: fidl_config.monitor_time_source_url,
monitor_uses_pull: fidl_config.monitor_uses_pull,
oscillator_error_std_dev_ppm: fidl_config.oscillator_error_std_dev_ppm,
power_topology_integration_enabled: fidl_config.power_topology_integration_enabled,
primary_time_source_url: fidl_config.primary_time_source_url,
primary_uses_pull: fidl_config.primary_uses_pull,
serve_fuchsia_time_alarms: fidl_config.serve_fuchsia_time_alarms,
serve_test_protocols: fidl_config.serve_test_protocols,
utc_start_at_startup: fidl_config.utc_start_at_startup,
utc_start_at_startup_when_invalid_rtc: fidl_config
.utc_start_at_startup_when_invalid_rtc,
}
}
pub fn record_inspect(&self, inspector_node: &Node) {
inspector_node.record_int(
"back_off_time_between_pull_samples_sec",
self.back_off_time_between_pull_samples_sec,
);
inspector_node.record_bool("disable_delays", self.disable_delays);
inspector_node.record_bool("early_exit", self.early_exit);
inspector_node.record_int("first_sampling_delay_sec", self.first_sampling_delay_sec);
inspector_node.record_bool("has_real_time_clock", self.has_real_time_clock);
inspector_node.record_uint("initial_frequency_ppm", self.initial_frequency_ppm as u64);
inspector_node.record_uint("max_frequency_error_ppm", self.max_frequency_error_ppm as u64);
inspector_node.record_string("monitor_time_source_url", &self.monitor_time_source_url);
inspector_node.record_bool("monitor_uses_pull", self.monitor_uses_pull);
inspector_node
.record_uint("oscillator_error_std_dev_ppm", self.oscillator_error_std_dev_ppm as u64);
inspector_node.record_bool(
"power_topology_integration_enabled",
self.power_topology_integration_enabled,
);
inspector_node.record_string("primary_time_source_url", &self.primary_time_source_url);
inspector_node.record_bool("primary_uses_pull", self.primary_uses_pull);
inspector_node.record_bool("serve_fuchsia_time_alarms", self.serve_fuchsia_time_alarms);
inspector_node.record_bool("serve_test_protocols", self.serve_test_protocols);
inspector_node.record_bool("utc_start_at_startup", self.utc_start_at_startup);
inspector_node.record_bool(
"utc_start_at_startup_when_invalid_rtc",
self.utc_start_at_startup_when_invalid_rtc,
);
}
}