1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use anyhow::{format_err, Error};
6use fidl_fuchsia_wlan_product_deprecatedconfiguration as fidl_deprecated;
7use fuchsia_component::client::connect_to_protocol;
8use fuchsia_sync::RwLock;
9use std::fmt::Debug;
1011#[derive(Debug)]
12struct InnerWlanDeprecatedConfigurationFacade {
13 controller: fidl_deprecated::DeprecatedConfiguratorProxy,
14}
1516#[derive(Debug)]
17pub struct WlanDeprecatedConfigurationFacade {
18 inner: RwLock<InnerWlanDeprecatedConfigurationFacade>,
19}
2021impl WlanDeprecatedConfigurationFacade {
22/// Connects to the DeprecatedConfigurator service and returns a
23 /// WlanDeprecatedConfigurationFacade instance to enable setting suggested MAC addresses for
24 /// soft APs.
25pub fn new() -> Result<WlanDeprecatedConfigurationFacade, Error> {
26let controller = connect_to_protocol::<fidl_deprecated::DeprecatedConfiguratorMarker>()?;
27Ok(Self {
28 inner: RwLock::new(InnerWlanDeprecatedConfigurationFacade { controller: controller }),
29 })
30 }
3132/// Communicates with the DeprecatedConfigurator service to set a preferred MAC address to be
33 /// used for new soft APs. This API waits for an acknowledgement from the
34 /// DeprecatedConfigurator service indicating that the suggested MAC address has been set.
35 ///
36 /// # Arguments
37 ///
38 /// `mac` - A MAC address in the form of an eui48::MacAddress.
39pub async fn suggest_access_point_mac_address(
40&self,
41 mac: eui48::MacAddress,
42 ) -> Result<(), Error> {
43let inner_guard = self.inner.read();
44let controller = &inner_guard.controller;
4546let mac = fidl_fuchsia_net::MacAddress { octets: mac.to_array() };
47let result = controller.suggest_access_point_mac_address(&mac).await?;
48 result.map_err(|e| format_err!("could not set preferred MAC: {:?}", e))
49 }
5051/// Consumes the arguments from an ACTS test, looks for a "mac" key, and attempts to convert
52 /// the associated value to a MAC address.
53 ///
54 /// # Arguments
55 ///
56 /// * `args` - A JSON blob represented as a serde_json::Value containing a "mac" key.
57pub fn parse_mac_argument(&self, args: serde_json::Value) -> Result<eui48::MacAddress, Error> {
58let mac = match args.get("mac") {
59Some(mac) => match mac.as_str() {
60Some(mac) => eui48::MacAddress::parse_str(mac)?,
61None => {
62return Err(format_err!(
63"MAC address could not be interpreted as a string: {:?}",
64 mac
65 ))
66 }
67 },
68None => return Err(format_err!("Please provide a preferred MAC")),
69 };
70Ok(mac)
71 }
72}