wlantap_client/
lib.rs

1// Copyright 2018 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::Error;
6use {fidl_fuchsia_io as fio, fidl_fuchsia_wlan_tap as wlantap};
7
8pub struct Wlantap {
9    proxy: wlantap::WlantapCtlProxy,
10}
11
12impl Wlantap {
13    pub async fn open() -> Result<Self, Error> {
14        let dir = fuchsia_fs::directory::open_in_namespace("/dev", fuchsia_fs::Flags::empty())?;
15        Self::open_from_devfs(&dir).await
16    }
17
18    pub async fn open_from_devfs(devfs: &fio::DirectoryProxy) -> Result<Self, Error> {
19        let proxy = device_watcher::recursive_wait_and_open::<wlantap::WlantapCtlMarker>(
20            devfs,
21            "sys/test/wlantapctl",
22        )
23        .await?;
24        Ok(Self { proxy })
25    }
26
27    pub async fn create_phy(
28        &self,
29        config: wlantap::WlantapPhyConfig,
30    ) -> Result<wlantap::WlantapPhyProxy, Error> {
31        let Self { proxy } = self;
32        let (ours, theirs) = fidl::endpoints::create_proxy();
33
34        let status = proxy.create_phy(&config, theirs).await?;
35        let () = zx::ok(status)?;
36
37        Ok(ours)
38    }
39}