sl4f_lib/hwinfo/
facade.rs

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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::common_utils::common::macros::{fx_err_and_bail, with_line};
use crate::hwinfo::types::{
    SerializableBoardInfo, SerializableDeviceInfo, SerializableProductInfo,
};
use anyhow::Error;
use fidl_fuchsia_hwinfo::{BoardMarker, DeviceMarker, ProductMarker};
use fuchsia_component as app;
use tracing::info;

/// Perform HwInfo fidl operations.
///
/// Note this object is shared among all threads created by server.
///
#[derive(Debug)]
pub struct HwinfoFacade {}

impl HwinfoFacade {
    pub fn new() -> HwinfoFacade {
        HwinfoFacade {}
    }

    /// Returns the device info of the hwinfo proxy service. Currently
    /// only returns the serial number.
    pub async fn get_device_info(&self) -> Result<SerializableDeviceInfo, Error> {
        let tag = "HwinfoFacade::get_info";

        let device_info_proxy = app::client::connect_to_protocol::<DeviceMarker>();

        match device_info_proxy {
            Ok(p) => {
                let device_info = p.get_info().await?;
                let device_info_string = format!("Device info found: {:?}", device_info);
                info!(tag = &with_line!(tag), "{}", device_info_string);
                Ok(SerializableDeviceInfo::new(&device_info))
            }
            Err(err) => fx_err_and_bail!(
                &with_line!(tag),
                format_err!("Failed to create hwinfo proxy: {}", err)
            ),
        }
    }

    /// Returns the device info of the product info proxy service.
    pub async fn get_product_info(&self) -> Result<SerializableProductInfo, Error> {
        let tag = "HwinfoFacade::get_product_info";

        let product_info_proxy = app::client::connect_to_protocol::<ProductMarker>();

        match product_info_proxy {
            Ok(p) => {
                let product_info = p.get_info().await?;
                let product_info_string = format!("Product info found: {:?}", product_info);
                info!(tag = &with_line!(tag), "{}", product_info_string);
                Ok(SerializableProductInfo::new(&product_info))
            }
            Err(err) => fx_err_and_bail!(
                &with_line!(tag),
                format_err!("Failed to create hwinfo proxy: {}", err)
            ),
        }
    }

    /// Returns the board info of the hwinfo proxy service.
    pub async fn get_board_info(&self) -> Result<SerializableBoardInfo, Error> {
        let tag = "HwinfoFacade::get_board_info";
        let board_info_proxy = app::client::connect_to_protocol::<BoardMarker>();
        match board_info_proxy {
            Ok(p) => {
                let board_info = p.get_info().await?;
                let board_info_string = format!("Board info found: {:?}", board_info);
                info!(tag = &with_line!(tag), "{}", board_info_string);
                Ok(SerializableBoardInfo::new(&board_info))
            }
            Err(err) => fx_err_and_bail!(
                &with_line!(tag),
                format_err!("Failed to create hwinfo proxy: {}", err)
            ),
        }
    }
}