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
// 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 anyhow::{format_err, Context, Error};
use fidl_fuchsia_wlan_device_service::{DeviceMonitorMarker, DeviceMonitorProxy};
use fuchsia_component::client::connect_to_protocol;
use fuchsia_zircon as zx;

#[derive(Debug)]
pub struct WlanPhyFacade {
    device_monitor: DeviceMonitorProxy,
}

impl WlanPhyFacade {
    pub fn new() -> Result<WlanPhyFacade, Error> {
        Ok(WlanPhyFacade { device_monitor: connect_to_protocol::<DeviceMonitorMarker>()? })
    }

    /// Queries the currently counfigured country from phy `phy_id`.
    ///
    /// # Arguments
    /// * `phy_id`: a u16 id representing the phy
    pub async fn get_country(&self, phy_id: u16) -> Result<[u8; 2], Error> {
        let country_code = self
            .device_monitor
            .get_country(phy_id)
            .await
            .context("get_country(): encountered FIDL error")?;
        match country_code {
            Ok(country) => Ok(country.alpha2),
            Err(status) => Err(format_err!(
                "get_country(): encountered service failure {}",
                zx::Status::from_raw(status)
            )),
        }
    }

    /// Queries the device path of the PHY specified by `phy_id`.
    ///
    /// # Arguments
    /// * `phy_id`: a u16 id representing the phy
    pub async fn get_dev_path(&self, phy_id: u16) -> Result<Option<String>, Error> {
        self.device_monitor
            .get_dev_path(phy_id)
            .await
            .map_err(|e| format_err!("get_path(): encountered FIDL error: {:?}", e))
    }
}