brightness_manager_config/
brightness_manager_config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_brightnessmanagerconfig::Config as FidlConfig;
3use fuchsia_inspect::Node;
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0xe0, 0xaa, 0x2e, 0x12, 0xcb, 0x94, 0x4e, 0xb0, 0x38, 0x64, 0x81, 0xdb, 0x40, 0x90, 0xf0, 0xe3,
8    0x0f, 0xa3, 0xf5, 0x1c, 0xcb, 0x9b, 0x0d, 0x21, 0x5c, 0x29, 0xd8, 0xb7, 0xc5, 0xeb, 0x11, 0xf9,
9];
10#[derive(Debug)]
11pub struct Config {
12    pub manage_display_power: bool,
13    pub power_off_delay_millis: u16,
14    pub power_on_delay_millis: u16,
15}
16impl Config {
17    #[doc = r" Take the config startup handle and parse its contents."]
18    #[doc = r""]
19    #[doc = r" # Panics"]
20    #[doc = r""]
21    #[doc = r" If the config startup handle was already taken or if it is not valid."]
22    pub fn take_from_startup_handle() -> Self {
23        let handle_info = HandleInfo::new(HandleType::ComponentConfigVmo, 0);
24        let config_vmo: zx::Vmo =
25            take_startup_handle(handle_info).expect("Config VMO handle must be present.").into();
26        Self::from_vmo(&config_vmo).expect("Config VMO handle must be valid.")
27    }
28    #[doc = r" Parse `Self` from `vmo`."]
29    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
30        let config_size = vmo.get_content_size().map_err(Error::GettingContentSize)?;
31        let config_bytes = vmo.read_to_vec(0, config_size).map_err(Error::ReadingConfigBytes)?;
32        Self::from_bytes(&config_bytes)
33    }
34    #[doc = r" Parse `Self` from `bytes`."]
35    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
36        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
37        let checksum_len_bytes: [u8; 2] =
38            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
39        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
40        let (observed_checksum, bytes) =
41            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
42        if observed_checksum != EXPECTED_CHECKSUM {
43            return Err(Error::ChecksumMismatch { observed_checksum: observed_checksum.to_vec() });
44        }
45        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
46        Ok(Self {
47            manage_display_power: fidl_config.manage_display_power,
48            power_off_delay_millis: fidl_config.power_off_delay_millis,
49            power_on_delay_millis: fidl_config.power_on_delay_millis,
50        })
51    }
52    pub fn record_inspect(&self, inspector_node: &Node) {
53        inspector_node.record_bool("manage_display_power", self.manage_display_power);
54        inspector_node.record_uint("power_off_delay_millis", self.power_off_delay_millis as u64);
55        inspector_node.record_uint("power_on_delay_millis", self.power_on_delay_millis as u64);
56    }
57}
58#[derive(Debug)]
59pub enum Error {
60    #[doc = r" Failed to read the content size of the VMO."]
61    GettingContentSize(zx::Status),
62    #[doc = r" Failed to read the content of the VMO."]
63    ReadingConfigBytes(zx::Status),
64    #[doc = r" The VMO was too small for this config library."]
65    TooFewBytes,
66    #[doc = r" The VMO's config ABI checksum did not match this library's."]
67    ChecksumMismatch { observed_checksum: Vec<u8> },
68    #[doc = r" Failed to parse the non-checksum bytes of the VMO as this library's FIDL type."]
69    Unpersist(fidl::Error),
70}
71impl std::fmt::Display for Error {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        match self {
74            Self::GettingContentSize(status) => {
75                write!(f, "Failed to get content size: {status}")
76            }
77            Self::ReadingConfigBytes(status) => {
78                write!(f, "Failed to read VMO content: {status}")
79            }
80            Self::TooFewBytes => {
81                write!(f, "VMO content is not large enough for this config library.")
82            }
83            Self::ChecksumMismatch { observed_checksum } => {
84                write!(
85                    f,
86                    "ABI checksum mismatch, expected {:?}, got {:?}",
87                    EXPECTED_CHECKSUM, observed_checksum,
88                )
89            }
90            Self::Unpersist(fidl_error) => {
91                write!(f, "Failed to parse contents of config VMO: {fidl_error}")
92            }
93        }
94    }
95}
96impl std::error::Error for Error {
97    #[allow(unused_parens, reason = "rustfmt errors without parens here")]
98    fn source(&self) -> Option<(&'_ (dyn std::error::Error + 'static))> {
99        match self {
100            Self::GettingContentSize(ref status) | Self::ReadingConfigBytes(ref status) => {
101                Some(status)
102            }
103            Self::TooFewBytes => None,
104            Self::ChecksumMismatch { .. } => None,
105            Self::Unpersist(ref fidl_error) => Some(fidl_error),
106        }
107    }
108    fn description(&self) -> &str {
109        match self {
110            Self::GettingContentSize(_) => "getting content size",
111            Self::ReadingConfigBytes(_) => "reading VMO contents",
112            Self::TooFewBytes => "VMO contents too small",
113            Self::ChecksumMismatch { .. } => "ABI checksum mismatch",
114            Self::Unpersist(_) => "FIDL parsing error",
115        }
116    }
117}