bt_gap_config/
bt_gap_config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_btgapconfig::Config as FidlConfig;
3use fuchsia_inspect::Node;
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5#[derive(Debug)]
6pub struct Config {
7    pub bredr_connectable: bool,
8    pub bredr_security_mode: String,
9    pub le_background_scanning: bool,
10    pub le_privacy: bool,
11    pub le_security_mode: String,
12}
13impl Config {
14    pub fn take_from_startup_handle() -> Self {
15        let config_vmo: zx::Vmo =
16            take_startup_handle(HandleInfo::new(HandleType::ComponentConfigVmo, 0))
17                .expect("Config VMO handle must be provided and cannot already have been taken.")
18                .into();
19        let config_size =
20            config_vmo.get_content_size().expect("must be able to read config vmo content size");
21        assert_ne!(config_size, 0, "config vmo must be non-empty");
22        let config_bytes =
23            config_vmo.read_to_vec(0, config_size).expect("must be able to read config vmo");
24        let checksum_length = u16::from_le_bytes([config_bytes[0], config_bytes[1]]) as usize;
25        let fidl_start = 2 + checksum_length;
26        let observed_checksum = &config_bytes[2..fidl_start];
27        let expected_checksum = vec![
28            0x1c, 0xbb, 0x3f, 0x24, 0x89, 0x00, 0x00, 0x5d, 0x4c, 0xd8, 0x73, 0xbc, 0x20, 0x94,
29            0xec, 0x68, 0x95, 0xd6, 0xf3, 0x1a, 0x90, 0x90, 0xdb, 0x99, 0x5e, 0xc2, 0xf7, 0xa0,
30            0x90, 0x50, 0x34, 0x31,
31        ];
32        assert_eq!(
33            observed_checksum, expected_checksum,
34            "checksum from config VMO does not match expected checksum"
35        );
36        let fidl_config: FidlConfig = unpersist(&config_bytes[fidl_start..])
37            .expect("must be able to parse bytes as config FIDL");
38        Self {
39            bredr_connectable: fidl_config.bredr_connectable,
40            bredr_security_mode: fidl_config.bredr_security_mode,
41            le_background_scanning: fidl_config.le_background_scanning,
42            le_privacy: fidl_config.le_privacy,
43            le_security_mode: fidl_config.le_security_mode,
44        }
45    }
46    pub fn record_inspect(&self, inspector_node: &Node) {
47        inspector_node.record_bool("bredr_connectable", self.bredr_connectable);
48        inspector_node.record_string("bredr_security_mode", &self.bredr_security_mode);
49        inspector_node.record_bool("le_background_scanning", self.le_background_scanning);
50        inspector_node.record_bool("le_privacy", self.le_privacy);
51        inspector_node.record_string("le_security_mode", &self.le_security_mode);
52    }
53}