wifi_ctrl/ap/
types.rs

1use super::{error, Result};
2use serde::{de, Deserialize, Serialize};
3
4/// Status of the WiFi Station
5#[derive(Serialize, Deserialize, Debug)]
6pub struct Status {
7    pub state: String,
8    pub phy: String,
9    pub freq: String,
10    pub num_sta_non_erp: String,
11    pub num_sta_no_short_slot_time: String,
12    pub num_sta_no_short_preamble: String,
13    pub olbc: String,
14    pub num_sta_ht_no_gf: String,
15    pub num_sta_no_ht: String,
16    pub num_sta_ht_20_mhz: String,
17    pub num_sta_ht40_intolerant: String,
18    pub olbc_ht: String,
19    pub ht_op_mode: String,
20    pub cac_time_seconds: String,
21    pub cac_time_left_seconds: String,
22    pub channel: String,
23    pub secondary_channel: String,
24    pub ieee80211n: String,
25    pub ieee80211ac: String,
26    pub ieee80211ax: String,
27    pub beacon_int: String,
28    pub dtim_period: String,
29    pub ht_caps_info: String,
30    pub ht_mcs_bitmask: String,
31    pub supported_rates: String,
32    pub max_txpower: String,
33    pub bss: Vec<String>,
34    pub bssid: Vec<String>,
35    pub ssid: Vec<String>,
36    pub num_sta: Vec<String>,
37}
38
39impl Status {
40    pub fn from_response(response: &str) -> Result<Status> {
41        use config::{Config, File, FileFormat};
42        let config = Config::builder()
43            .add_source(File::from_str(response, FileFormat::Ini))
44            .build()
45            .map_err(|e| error::Error::ParsingWifiStatus {
46                e,
47                s: response.into(),
48            })?;
49
50        Ok(config.try_deserialize::<Status>().unwrap())
51    }
52}
53
54/// Configuration of the WiFi station
55#[derive(Serialize, Deserialize, Debug)]
56pub struct Config {
57    pub bssid: String,
58    pub ssid: String,
59    #[serde(deserialize_with = "deserialize_enabled_bool")]
60    pub wps_state: bool,
61    pub wpa: i32,
62    pub ket_mgmt: String,
63    pub group_cipher: String,
64    pub rsn_pairwise_cipher: String,
65    pub wpa_pairwise_cipher: String,
66}
67
68impl Config {
69    pub fn from_response(response: &str) -> Result<Config> {
70        use config::{File, FileFormat};
71        let config = config::Config::builder()
72            .add_source(File::from_str(response, FileFormat::Ini))
73            .build()
74            .map_err(|e| error::Error::ParsingWifiConfig {
75                e,
76                s: response.into(),
77            })?;
78
79        Ok(config.try_deserialize::<Config>().unwrap())
80    }
81}
82
83fn deserialize_enabled_bool<'de, D>(deserializer: D) -> std::result::Result<bool, D::Error>
84where
85    D: de::Deserializer<'de>,
86{
87    let s: &str = de::Deserialize::deserialize(deserializer)?;
88
89    match s {
90        "enabled" => Ok(true),
91        "disabled" => Ok(false),
92        _ => Err(de::Error::unknown_variant(s, &["enabled", "disabled"])),
93    }
94}