wlancfg_lib/legacy/
mod.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::{format_err, Error};
6use fidl_fuchsia_wlan_sme as fidl_sme;
7use fuchsia_sync::Mutex;
8use std::sync::Arc;
9
10pub mod deprecated_client;
11pub mod deprecated_configuration;
12
13#[derive(Clone)]
14pub struct Iface {
15    pub sme: fidl_sme::ClientSmeProxy,
16    pub iface_id: u16,
17}
18
19#[derive(Clone)]
20pub struct IfaceRef(Arc<Mutex<Option<Iface>>>);
21impl Default for IfaceRef {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl IfaceRef {
28    pub fn new() -> Self {
29        IfaceRef(Arc::new(Mutex::new(None)))
30    }
31    pub fn set_if_empty(&self, iface: Iface) {
32        let mut c = self.0.lock();
33        if c.is_none() {
34            *c = Some(iface);
35        }
36    }
37    pub fn remove_if_matching(&self, iface_id: u16) {
38        let mut c = self.0.lock();
39        let same_id = match *c {
40            Some(ref c) => c.iface_id == iface_id,
41            None => false,
42        };
43        if same_id {
44            *c = None;
45        }
46    }
47    pub fn get(&self) -> Result<Iface, Error> {
48        self.0.lock().clone().ok_or_else(|| format_err!("no available client interfaces"))
49    }
50}