Skip to main content

bt_host_config/
bt_host_config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_bthostconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0xa7, 0xb0, 0xc2, 0x77, 0xc1, 0xe4, 0x8a, 0xc4, 0x95, 0xb5, 0xc8, 0x54, 0xcf, 0x22, 0xdf, 0xb3,
8    0xc1, 0xff, 0x15, 0xcc, 0x4b, 0x36, 0x95, 0x35, 0x42, 0x97, 0xe2, 0x4d, 0x12, 0x0c, 0xcf, 0x3d,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub device_path: String,
14    pub enable_suspend: bool,
15    pub hci_command_timeout_seconds: u16,
16    pub le_active_scan_interval: u16,
17    pub le_active_scan_window: u16,
18    pub le_fast_adv_interval_max: u16,
19    pub le_fast_adv_interval_min: u16,
20    pub le_fast_adv_max_tx_power: i8,
21    pub le_slow_adv_interval_max: u16,
22    pub le_slow_adv_interval_min: u16,
23    pub le_slow_adv_max_tx_power: i8,
24    pub le_very_fast_adv_interval_max: u16,
25    pub le_very_fast_adv_interval_min: u16,
26    pub le_very_fast_adv_max_tx_power: i8,
27    pub legacy_pairing_enabled: bool,
28    pub override_vendor_capabilites_version: u16,
29    pub sco_offload_path_index: u8,
30}
31impl Config {
32    #[doc = r" Take the config startup handle and parse its contents."]
33    #[doc = r""]
34    #[doc = r" # Panics"]
35    #[doc = r""]
36    #[doc = r" If the config startup handle was already taken or if it is not valid."]
37    pub fn take_from_startup_handle() -> Self {
38        <Self as ComponentConfig>::take_from_startup_handle()
39    }
40    #[doc = r" Parse `Self` from `vmo`."]
41    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
42        <Self as ComponentConfig>::from_vmo(vmo)
43    }
44    #[doc = r" Parse `Self` from `bytes`."]
45    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
46        <Self as ComponentConfig>::from_bytes(bytes)
47    }
48    pub fn record_inspect(&self, inspector_node: &Node) {
49        <Self as ComponentConfig>::record_inspect(self, inspector_node)
50    }
51}
52impl ComponentConfig for Config {
53    #[doc = r" Parse `Self` from `bytes`."]
54    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 {
63                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
64                observed_checksum: observed_checksum.to_vec(),
65            });
66        }
67        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
68        Ok(Self {
69            device_path: fidl_config.device_path,
70            enable_suspend: fidl_config.enable_suspend,
71            hci_command_timeout_seconds: fidl_config.hci_command_timeout_seconds,
72            le_active_scan_interval: fidl_config.le_active_scan_interval,
73            le_active_scan_window: fidl_config.le_active_scan_window,
74            le_fast_adv_interval_max: fidl_config.le_fast_adv_interval_max,
75            le_fast_adv_interval_min: fidl_config.le_fast_adv_interval_min,
76            le_fast_adv_max_tx_power: fidl_config.le_fast_adv_max_tx_power,
77            le_slow_adv_interval_max: fidl_config.le_slow_adv_interval_max,
78            le_slow_adv_interval_min: fidl_config.le_slow_adv_interval_min,
79            le_slow_adv_max_tx_power: fidl_config.le_slow_adv_max_tx_power,
80            le_very_fast_adv_interval_max: fidl_config.le_very_fast_adv_interval_max,
81            le_very_fast_adv_interval_min: fidl_config.le_very_fast_adv_interval_min,
82            le_very_fast_adv_max_tx_power: fidl_config.le_very_fast_adv_max_tx_power,
83            legacy_pairing_enabled: fidl_config.legacy_pairing_enabled,
84            override_vendor_capabilites_version: fidl_config.override_vendor_capabilites_version,
85            sco_offload_path_index: fidl_config.sco_offload_path_index,
86        })
87    }
88    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
89        let fidl_config = FidlConfig {
90            device_path: self.device_path.clone(),
91            enable_suspend: self.enable_suspend.clone(),
92            hci_command_timeout_seconds: self.hci_command_timeout_seconds.clone(),
93            le_active_scan_interval: self.le_active_scan_interval.clone(),
94            le_active_scan_window: self.le_active_scan_window.clone(),
95            le_fast_adv_interval_max: self.le_fast_adv_interval_max.clone(),
96            le_fast_adv_interval_min: self.le_fast_adv_interval_min.clone(),
97            le_fast_adv_max_tx_power: self.le_fast_adv_max_tx_power.clone(),
98            le_slow_adv_interval_max: self.le_slow_adv_interval_max.clone(),
99            le_slow_adv_interval_min: self.le_slow_adv_interval_min.clone(),
100            le_slow_adv_max_tx_power: self.le_slow_adv_max_tx_power.clone(),
101            le_very_fast_adv_interval_max: self.le_very_fast_adv_interval_max.clone(),
102            le_very_fast_adv_interval_min: self.le_very_fast_adv_interval_min.clone(),
103            le_very_fast_adv_max_tx_power: self.le_very_fast_adv_max_tx_power.clone(),
104            legacy_pairing_enabled: self.legacy_pairing_enabled.clone(),
105            override_vendor_capabilites_version: self.override_vendor_capabilites_version.clone(),
106            sco_offload_path_index: self.sco_offload_path_index.clone(),
107        };
108        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
109        let mut bytes = Vec::with_capacity(
110            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
111        );
112        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
113        bytes.extend_from_slice(EXPECTED_CHECKSUM);
114        bytes.append(&mut fidl_bytes);
115        Ok(bytes)
116    }
117    fn record_inspect(&self, inspector_node: &Node) {
118        inspector_node.record_string("device_path", &self.device_path);
119        inspector_node.record_bool("enable_suspend", self.enable_suspend);
120        inspector_node
121            .record_uint("hci_command_timeout_seconds", self.hci_command_timeout_seconds as u64);
122        inspector_node.record_uint("le_active_scan_interval", self.le_active_scan_interval as u64);
123        inspector_node.record_uint("le_active_scan_window", self.le_active_scan_window as u64);
124        inspector_node
125            .record_uint("le_fast_adv_interval_max", self.le_fast_adv_interval_max as u64);
126        inspector_node
127            .record_uint("le_fast_adv_interval_min", self.le_fast_adv_interval_min as u64);
128        inspector_node.record_int("le_fast_adv_max_tx_power", self.le_fast_adv_max_tx_power as i64);
129        inspector_node
130            .record_uint("le_slow_adv_interval_max", self.le_slow_adv_interval_max as u64);
131        inspector_node
132            .record_uint("le_slow_adv_interval_min", self.le_slow_adv_interval_min as u64);
133        inspector_node.record_int("le_slow_adv_max_tx_power", self.le_slow_adv_max_tx_power as i64);
134        inspector_node.record_uint(
135            "le_very_fast_adv_interval_max",
136            self.le_very_fast_adv_interval_max as u64,
137        );
138        inspector_node.record_uint(
139            "le_very_fast_adv_interval_min",
140            self.le_very_fast_adv_interval_min as u64,
141        );
142        inspector_node
143            .record_int("le_very_fast_adv_max_tx_power", self.le_very_fast_adv_max_tx_power as i64);
144        inspector_node.record_bool("legacy_pairing_enabled", self.legacy_pairing_enabled);
145        inspector_node.record_uint(
146            "override_vendor_capabilites_version",
147            self.override_vendor_capabilites_version as u64,
148        );
149        inspector_node.record_uint("sco_offload_path_index", self.sco_offload_path_index as u64);
150    }
151}