• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /// Configuration for netsim
16 use lazy_static::lazy_static;
17 use std::sync::{Once, RwLock};
18 
19 static SET_DEV_CALLED: Once = Once::new();
20 static SET_PCAP_CALLED: Once = Once::new();
21 static SET_DISABLE_WIFI_P2P_CALLED: Once = Once::new();
22 
23 lazy_static! {
24     static ref CONFIG: RwLock<Config> = RwLock::new(Config::new());
25 }
26 
27 struct Config {
28     pub dev: Option<bool>,
29     pub pcap: Option<bool>,
30     pub disable_wifi_p2p: Option<bool>,
31 }
32 
33 impl Config {
new() -> Self34     pub fn new() -> Self {
35         Self { dev: None, pcap: None, disable_wifi_p2p: None }
36     }
37 }
38 
39 /// Get the flag of dev
get_dev() -> bool40 pub fn get_dev() -> bool {
41     let config = CONFIG.read().unwrap();
42     config.dev.unwrap_or(false)
43 }
44 
45 /// Set the flag of dev
set_dev(flag: bool)46 pub fn set_dev(flag: bool) {
47     SET_DEV_CALLED.call_once(|| {
48         let mut config = CONFIG.write().unwrap();
49         config.dev = Some(flag);
50     });
51 }
52 
53 /// Get the flag of pcap
get_pcap() -> bool54 pub fn get_pcap() -> bool {
55     let config = CONFIG.read().unwrap();
56     config.pcap.unwrap_or(false)
57 }
58 
59 /// Set the flag of pcap
set_pcap(flag: bool)60 pub fn set_pcap(flag: bool) {
61     SET_PCAP_CALLED.call_once(|| {
62         let mut config = CONFIG.write().unwrap();
63         config.pcap = Some(flag);
64     });
65 }
66 
67 /// Get the flag of disable_wifi_p2p
get_disable_wifi_p2p() -> bool68 pub fn get_disable_wifi_p2p() -> bool {
69     let config = CONFIG.read().unwrap();
70     config.disable_wifi_p2p.unwrap_or(false)
71 }
72 
73 /// Set the flag of disable_wifi_p2p
set_disable_wifi_p2p(flag: bool)74 pub fn set_disable_wifi_p2p(flag: bool) {
75     SET_DISABLE_WIFI_P2P_CALLED.call_once(|| {
76         let mut config = CONFIG.write().unwrap();
77         config.disable_wifi_p2p = Some(flag);
78     });
79 }
80 
81 #[cfg(test)]
82 mod tests {
83     use super::*;
84 
85     #[test]
test_dev()86     fn test_dev() {
87         // Check if default dev boolean is false
88         assert!(!get_dev());
89 
90         // Check if set_dev changes the flag to true
91         set_dev(true);
92         assert!(get_dev());
93 
94         // Check if set_dev can only be called once
95         set_dev(false);
96         assert!(get_dev());
97     }
98 
99     #[test]
test_pcap()100     fn test_pcap() {
101         // Check if default pcap boolean is false
102         assert!(!get_pcap());
103 
104         // Check if set_pcap changes the flag to true
105         set_pcap(true);
106         assert!(get_pcap());
107 
108         // Check if set_pcap can only be called once
109         set_pcap(false);
110         assert!(get_pcap());
111     }
112 
113     #[test]
test_disable_wifi_p2p()114     fn test_disable_wifi_p2p() {
115         // Check if default disable_wifi_p2p boolean is false
116         assert!(!get_disable_wifi_p2p());
117 
118         // Check if set_disable_wifi_p2p changes the flag to true
119         set_disable_wifi_p2p(true);
120         assert!(get_disable_wifi_p2p());
121 
122         // Check if set_disable_wifi_p2p can only be called once
123         set_disable_wifi_p2p(false);
124         assert!(get_disable_wifi_p2p());
125     }
126 }
127