Skip to main content

a2dp_profile_config/
a2dp_profile_config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_a2dpprofileconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0x86, 0xf7, 0xab, 0x7e, 0x80, 0xb6, 0x5f, 0x16, 0x55, 0xf7, 0xf1, 0x51, 0x01, 0xd5, 0xd1, 0x1e,
8    0xb5, 0x19, 0xbb, 0xa3, 0xc4, 0x27, 0x1e, 0xd5, 0xfd, 0x77, 0xfc, 0xa2, 0x47, 0x24, 0x96, 0x85,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub channel_mode: String,
14    pub domain: String,
15    pub enable_aac: bool,
16    pub enable_avrcp_target: bool,
17    pub enable_sink: bool,
18    pub initiator_delay: u32,
19    pub source_type: String,
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        <Self as ComponentConfig>::take_from_startup_handle()
29    }
30    #[doc = r" Parse `Self` from `vmo`."]
31    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
32        <Self as ComponentConfig>::from_vmo(vmo)
33    }
34    #[doc = r" Parse `Self` from `bytes`."]
35    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
36        <Self as ComponentConfig>::from_bytes(bytes)
37    }
38    pub fn record_inspect(&self, inspector_node: &Node) {
39        <Self as ComponentConfig>::record_inspect(self, inspector_node)
40    }
41}
42impl ComponentConfig for Config {
43    #[doc = r" Parse `Self` from `bytes`."]
44    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
45        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
46        let checksum_len_bytes: [u8; 2] =
47            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
48        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
49        let (observed_checksum, bytes) =
50            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
51        if observed_checksum != EXPECTED_CHECKSUM {
52            return Err(Error::ChecksumMismatch {
53                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
54                observed_checksum: observed_checksum.to_vec(),
55            });
56        }
57        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
58        Ok(Self {
59            channel_mode: fidl_config.channel_mode,
60            domain: fidl_config.domain,
61            enable_aac: fidl_config.enable_aac,
62            enable_avrcp_target: fidl_config.enable_avrcp_target,
63            enable_sink: fidl_config.enable_sink,
64            initiator_delay: fidl_config.initiator_delay,
65            source_type: fidl_config.source_type,
66        })
67    }
68    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
69        let fidl_config = FidlConfig {
70            channel_mode: self.channel_mode.clone(),
71            domain: self.domain.clone(),
72            enable_aac: self.enable_aac.clone(),
73            enable_avrcp_target: self.enable_avrcp_target.clone(),
74            enable_sink: self.enable_sink.clone(),
75            initiator_delay: self.initiator_delay.clone(),
76            source_type: self.source_type.clone(),
77        };
78        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
79        let mut bytes = Vec::with_capacity(
80            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
81        );
82        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
83        bytes.extend_from_slice(EXPECTED_CHECKSUM);
84        bytes.append(&mut fidl_bytes);
85        Ok(bytes)
86    }
87    fn record_inspect(&self, inspector_node: &Node) {
88        inspector_node.record_string("channel_mode", &self.channel_mode);
89        inspector_node.record_string("domain", &self.domain);
90        inspector_node.record_bool("enable_aac", self.enable_aac);
91        inspector_node.record_bool("enable_avrcp_target", self.enable_avrcp_target);
92        inspector_node.record_bool("enable_sink", self.enable_sink);
93        inspector_node.record_uint("initiator_delay", self.initiator_delay as u64);
94        inspector_node.record_string("source_type", &self.source_type);
95    }
96}