sl4f_lib/common_utils/
lowpan_context.rs

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.
4
5use anyhow::{Context as _, Error};
6use fidl::endpoints::create_endpoints;
7use fidl_fuchsia_factory_lowpan::{FactoryDeviceMarker, FactoryDeviceProxy, FactoryLookupMarker};
8use fidl_fuchsia_lowpan::{DeviceWatcherMarker, DeviceWatcherProxy};
9use fidl_fuchsia_lowpan_device::{
10    DeviceConnectorMarker, DeviceExtraConnectorMarker, DeviceExtraMarker, DeviceExtraProxy,
11    DeviceMarker, DeviceProxy,
12};
13use fidl_fuchsia_lowpan_test::{DeviceTestConnectorMarker, DeviceTestMarker, DeviceTestProxy};
14use fuchsia_component::client::connect_to_protocol;
15
16/// This struct contains all of the transient state that can
17/// be kept between invocations of commands when `lowpanctl` is
18/// invoked in interactive mode. For single command execution
19/// it is set up once and then discarded.
20pub struct LowpanContext {
21    pub device_watcher: DeviceWatcherProxy,
22    pub device_name: String,
23}
24
25impl LowpanContext {
26    const DEFAULT_DEVICE_NAME: &'static str = "lowpan0";
27
28    pub fn new(device_name: Option<String>) -> Result<LowpanContext, Error> {
29        let device_watcher = connect_to_protocol::<DeviceWatcherMarker>()
30            .context("Failed to connect to Lowpan DeviceWatcher service")?;
31
32        Ok(LowpanContext {
33            device_watcher,
34            device_name: device_name
35                .unwrap_or_else(|| LowpanContext::DEFAULT_DEVICE_NAME.to_string()),
36        })
37    }
38
39    /// Returns the default DeviceProxy.
40    pub async fn get_default_device(&self) -> Result<DeviceProxy, Error> {
41        let (client, server) = create_endpoints::<DeviceMarker>();
42
43        connect_to_protocol::<DeviceConnectorMarker>()
44            .context("Failed to connect to DeviceConnector")?
45            .connect(&self.device_name, server)?;
46
47        Ok(client.into_proxy())
48    }
49
50    /// Returns the default FactoryDeviceProxy.
51    pub async fn get_default_device_factory(&self) -> Result<FactoryDeviceProxy, Error> {
52        let lookup = connect_to_protocol::<FactoryLookupMarker>()
53            .context("Failed to connect to Lowpan FactoryLookup service")?;
54
55        let (client, server) = create_endpoints::<FactoryDeviceMarker>();
56
57        lookup.lookup(&self.device_name, server)?;
58
59        Ok(client.into_proxy())
60    }
61
62    /// Returns the default DeviceProxy, DeviceExtraProxy and DeviceTestProxy.
63    pub async fn get_default_device_proxies(
64        &self,
65    ) -> Result<(DeviceProxy, DeviceExtraProxy, DeviceTestProxy), Error> {
66        let (client, server) = create_endpoints::<DeviceMarker>();
67        let (client_extra, server_extra) = create_endpoints::<DeviceExtraMarker>();
68        let (client_test, server_test) = create_endpoints::<DeviceTestMarker>();
69
70        connect_to_protocol::<DeviceConnectorMarker>()
71            .context("Failed to connect to DeviceConnector")?
72            .connect(&self.device_name, server)?;
73
74        connect_to_protocol::<DeviceExtraConnectorMarker>()
75            .context("Failed to connect to DeviceExtraConnector")?
76            .connect(&self.device_name, server_extra)?;
77
78        connect_to_protocol::<DeviceTestConnectorMarker>()
79            .context("Failed to connect to DeviceTestConnector")?
80            .connect(&self.device_name, server_test)?;
81
82        Ok((client.into_proxy(), client_extra.into_proxy(), client_test.into_proxy()))
83    }
84}