use fidl::unpersist;
use fidl_cf_sc_internal_hfphandsfreeprofileconfig::Config as FidlConfig;
use fuchsia_inspect::Node;
use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
use fuchsia_zircon as zx;
#[derive(Debug)]
pub struct Config {
pub call_waiting_or_three_way_calling: bool,
pub cli_presentation_capability: bool,
pub ec_or_nr: bool,
pub enhanced_voice_recognition: bool,
pub enhanced_voice_recognition_with_text: bool,
pub remote_volume_control: bool,
pub voice_recognition_activation: bool,
pub wide_band_speech: 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 mut config_bytes = Vec::new();
config_bytes.resize(config_size as usize, 0);
config_vmo.read(&mut config_bytes, 0).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![
0x5f, 0xa4, 0xea, 0xc5, 0x38, 0x9c, 0xee, 0xe7, 0xd2, 0xb1, 0x58, 0x0b, 0xfc, 0x54,
0xe1, 0x1e, 0x47, 0xb1, 0xd3, 0x2f, 0xb3, 0x75, 0xee, 0x6b, 0x33, 0x16, 0xa2, 0x01,
0xfe, 0xc8, 0x96, 0xd9,
];
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 {
call_waiting_or_three_way_calling: fidl_config.call_waiting_or_three_way_calling,
cli_presentation_capability: fidl_config.cli_presentation_capability,
ec_or_nr: fidl_config.ec_or_nr,
enhanced_voice_recognition: fidl_config.enhanced_voice_recognition,
enhanced_voice_recognition_with_text: fidl_config.enhanced_voice_recognition_with_text,
remote_volume_control: fidl_config.remote_volume_control,
voice_recognition_activation: fidl_config.voice_recognition_activation,
wide_band_speech: fidl_config.wide_band_speech,
}
}
pub fn record_inspect(&self, inspector_node: &Node) {
inspector_node.record_bool(
"call_waiting_or_three_way_calling",
self.call_waiting_or_three_way_calling,
);
inspector_node.record_bool("cli_presentation_capability", self.cli_presentation_capability);
inspector_node.record_bool("ec_or_nr", self.ec_or_nr);
inspector_node.record_bool("enhanced_voice_recognition", self.enhanced_voice_recognition);
inspector_node.record_bool(
"enhanced_voice_recognition_with_text",
self.enhanced_voice_recognition_with_text,
);
inspector_node.record_bool("remote_volume_control", self.remote_volume_control);
inspector_node
.record_bool("voice_recognition_activation", self.voice_recognition_activation);
inspector_node.record_bool("wide_band_speech", self.wide_band_speech);
}
}