1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use fidl::unpersist;
use fidl_cf_sc_internal_hfpprofileconfig::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 attach_phone_number_to_voice_tag: bool,
    pub controller_encoding_cvsd: bool,
    pub controller_encoding_msbc: bool,
    pub echo_canceling_and_noise_reduction: bool,
    pub enhanced_call_controls: bool,
    pub enhanced_voice_recognition: bool,
    pub enhanced_voice_recognition_with_text: bool,
    pub in_band_ringtone: bool,
    pub reject_incoming_voice_call: bool,
    pub respond_and_hold: bool,
    pub three_way_calling: bool,
    pub voice_recognition: 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![
            0x82, 0x60, 0x41, 0x3e, 0x40, 0x42, 0x92, 0xd3, 0x6d, 0x0f, 0xbe, 0x34, 0x1e, 0x3a,
            0x05, 0x12, 0xb3, 0xf0, 0xe4, 0x98, 0xe1, 0x82, 0x14, 0xaa, 0xff, 0xf2, 0xdf, 0x7f,
            0x1c, 0xc7, 0x30, 0x4e,
        ];
        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 {
            attach_phone_number_to_voice_tag: fidl_config.attach_phone_number_to_voice_tag,
            controller_encoding_cvsd: fidl_config.controller_encoding_cvsd,
            controller_encoding_msbc: fidl_config.controller_encoding_msbc,
            echo_canceling_and_noise_reduction: fidl_config.echo_canceling_and_noise_reduction,
            enhanced_call_controls: fidl_config.enhanced_call_controls,
            enhanced_voice_recognition: fidl_config.enhanced_voice_recognition,
            enhanced_voice_recognition_with_text: fidl_config.enhanced_voice_recognition_with_text,
            in_band_ringtone: fidl_config.in_band_ringtone,
            reject_incoming_voice_call: fidl_config.reject_incoming_voice_call,
            respond_and_hold: fidl_config.respond_and_hold,
            three_way_calling: fidl_config.three_way_calling,
            voice_recognition: fidl_config.voice_recognition,
            wide_band_speech: fidl_config.wide_band_speech,
        }
    }
    pub fn record_inspect(&self, inspector_node: &Node) {
        inspector_node
            .record_bool("attach_phone_number_to_voice_tag", self.attach_phone_number_to_voice_tag);
        inspector_node.record_bool("controller_encoding_cvsd", self.controller_encoding_cvsd);
        inspector_node.record_bool("controller_encoding_msbc", self.controller_encoding_msbc);
        inspector_node.record_bool(
            "echo_canceling_and_noise_reduction",
            self.echo_canceling_and_noise_reduction,
        );
        inspector_node.record_bool("enhanced_call_controls", self.enhanced_call_controls);
        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("in_band_ringtone", self.in_band_ringtone);
        inspector_node.record_bool("reject_incoming_voice_call", self.reject_incoming_voice_call);
        inspector_node.record_bool("respond_and_hold", self.respond_and_hold);
        inspector_node.record_bool("three_way_calling", self.three_way_calling);
        inspector_node.record_bool("voice_recognition", self.voice_recognition);
        inspector_node.record_bool("wide_band_speech", self.wide_band_speech);
    }
}