hfp_hands_free_profile_config/
hfp_hands_free_profile_config_rust_config_lib_source.rs1use fidl::unpersist;
2use fidl_cf_sc_internal_hfphandsfreeprofileconfig::Config as FidlConfig;
3use fuchsia_inspect::Node;
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7 0x5f, 0xa4, 0xea, 0xc5, 0x38, 0x9c, 0xee, 0xe7, 0xd2, 0xb1, 0x58, 0x0b, 0xfc, 0x54, 0xe1, 0x1e,
8 0x47, 0xb1, 0xd3, 0x2f, 0xb3, 0x75, 0xee, 0x6b, 0x33, 0x16, 0xa2, 0x01, 0xfe, 0xc8, 0x96, 0xd9,
9];
10#[derive(Debug)]
11pub struct Config {
12 pub call_waiting_or_three_way_calling: bool,
13 pub cli_presentation_capability: bool,
14 pub ec_or_nr: bool,
15 pub enhanced_voice_recognition: bool,
16 pub enhanced_voice_recognition_with_text: bool,
17 pub remote_volume_control: bool,
18 pub voice_recognition_activation: bool,
19 pub wide_band_speech: bool,
20}
21impl Config {
22 #[doc = r" Take the config startup handle and parse its contents."]
23 #[doc = r""]
24 #[doc = r" # Panics"]
25 #[doc = r""]
26 #[doc = r" If the config startup handle was already taken or if it is not valid."]
27 pub fn take_from_startup_handle() -> Self {
28 let handle_info = HandleInfo::new(HandleType::ComponentConfigVmo, 0);
29 let config_vmo: zx::Vmo =
30 take_startup_handle(handle_info).expect("Config VMO handle must be present.").into();
31 Self::from_vmo(&config_vmo).expect("Config VMO handle must be valid.")
32 }
33 #[doc = r" Parse `Self` from `vmo`."]
34 pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
35 let config_size = vmo.get_content_size().map_err(Error::GettingContentSize)?;
36 let config_bytes = vmo.read_to_vec(0, config_size).map_err(Error::ReadingConfigBytes)?;
37 Self::from_bytes(&config_bytes)
38 }
39 #[doc = r" Parse `Self` from `bytes`."]
40 pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
41 let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
42 let checksum_len_bytes: [u8; 2] =
43 checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
44 let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
45 let (observed_checksum, bytes) =
46 bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
47 if observed_checksum != EXPECTED_CHECKSUM {
48 return Err(Error::ChecksumMismatch { observed_checksum: observed_checksum.to_vec() });
49 }
50 let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
51 Ok(Self {
52 call_waiting_or_three_way_calling: fidl_config.call_waiting_or_three_way_calling,
53 cli_presentation_capability: fidl_config.cli_presentation_capability,
54 ec_or_nr: fidl_config.ec_or_nr,
55 enhanced_voice_recognition: fidl_config.enhanced_voice_recognition,
56 enhanced_voice_recognition_with_text: fidl_config.enhanced_voice_recognition_with_text,
57 remote_volume_control: fidl_config.remote_volume_control,
58 voice_recognition_activation: fidl_config.voice_recognition_activation,
59 wide_band_speech: fidl_config.wide_band_speech,
60 })
61 }
62 pub fn record_inspect(&self, inspector_node: &Node) {
63 inspector_node.record_bool(
64 "call_waiting_or_three_way_calling",
65 self.call_waiting_or_three_way_calling,
66 );
67 inspector_node.record_bool("cli_presentation_capability", self.cli_presentation_capability);
68 inspector_node.record_bool("ec_or_nr", self.ec_or_nr);
69 inspector_node.record_bool("enhanced_voice_recognition", self.enhanced_voice_recognition);
70 inspector_node.record_bool(
71 "enhanced_voice_recognition_with_text",
72 self.enhanced_voice_recognition_with_text,
73 );
74 inspector_node.record_bool("remote_volume_control", self.remote_volume_control);
75 inspector_node
76 .record_bool("voice_recognition_activation", self.voice_recognition_activation);
77 inspector_node.record_bool("wide_band_speech", self.wide_band_speech);
78 }
79}
80#[derive(Debug)]
81pub enum Error {
82 #[doc = r" Failed to read the content size of the VMO."]
83 GettingContentSize(zx::Status),
84 #[doc = r" Failed to read the content of the VMO."]
85 ReadingConfigBytes(zx::Status),
86 #[doc = r" The VMO was too small for this config library."]
87 TooFewBytes,
88 #[doc = r" The VMO's config ABI checksum did not match this library's."]
89 ChecksumMismatch { observed_checksum: Vec<u8> },
90 #[doc = r" Failed to parse the non-checksum bytes of the VMO as this library's FIDL type."]
91 Unpersist(fidl::Error),
92}
93impl std::fmt::Display for Error {
94 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95 match self {
96 Self::GettingContentSize(status) => {
97 write!(f, "Failed to get content size: {status}")
98 }
99 Self::ReadingConfigBytes(status) => {
100 write!(f, "Failed to read VMO content: {status}")
101 }
102 Self::TooFewBytes => {
103 write!(f, "VMO content is not large enough for this config library.")
104 }
105 Self::ChecksumMismatch { observed_checksum } => {
106 write!(
107 f,
108 "ABI checksum mismatch, expected {:?}, got {:?}",
109 EXPECTED_CHECKSUM, observed_checksum,
110 )
111 }
112 Self::Unpersist(fidl_error) => {
113 write!(f, "Failed to parse contents of config VMO: {fidl_error}")
114 }
115 }
116 }
117}
118impl std::error::Error for Error {
119 #[allow(unused_parens, reason = "rustfmt errors without parens here")]
120 fn source(&self) -> Option<(&'_ (dyn std::error::Error + 'static))> {
121 match self {
122 Self::GettingContentSize(ref status) | Self::ReadingConfigBytes(ref status) => {
123 Some(status)
124 }
125 Self::TooFewBytes => None,
126 Self::ChecksumMismatch { .. } => None,
127 Self::Unpersist(ref fidl_error) => Some(fidl_error),
128 }
129 }
130 fn description(&self) -> &str {
131 match self {
132 Self::GettingContentSize(_) => "getting content size",
133 Self::ReadingConfigBytes(_) => "reading VMO contents",
134 Self::TooFewBytes => "VMO contents too small",
135 Self::ChecksumMismatch { .. } => "ABI checksum mismatch",
136 Self::Unpersist(_) => "FIDL parsing error",
137 }
138 }
139}