sl4f_lib/wlan/
types.rs

1// Copyright 2021 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 serde::{Deserialize, Serialize};
6use {
7    fidl_fuchsia_wlan_common as fidl_common, fidl_fuchsia_wlan_ieee80211 as fidl_ieee80211,
8    fidl_fuchsia_wlan_sme as fidl_sme,
9};
10
11/// Enums and structs for wlan client status.
12/// These definitions come from fuchsia.wlan.policy/client_provider.fidl
13///
14#[derive(Serialize, Deserialize, Debug)]
15pub enum WlanClientState {
16    ConnectionsDisabled = 1,
17    ConnectionsEnabled = 2,
18}
19
20#[derive(Serialize, Deserialize, Debug)]
21pub enum ConnectionState {
22    Failed = 1,
23    Disconnected = 2,
24    Connecting = 3,
25    Connected = 4,
26}
27
28#[derive(Serialize, Deserialize, Debug)]
29pub enum SecurityType {
30    None = 1,
31    Wep = 2,
32    Wpa = 3,
33    Wpa2 = 4,
34    Wpa3 = 5,
35}
36
37#[derive(Serialize, Deserialize, Debug)]
38pub enum DisconnectStatus {
39    TimedOut = 1,
40    CredentialsFailed = 2,
41    ConnectionStopped = 3,
42    ConnectionFailed = 4,
43}
44
45#[derive(Serialize, Deserialize, Debug)]
46pub struct NetworkIdentifier {
47    /// Network name, often used by users to choose between networks in the UI.
48    pub ssid: Vec<u8>,
49    /// Protection type (or not) for the network.
50    pub type_: SecurityType,
51}
52
53#[derive(Serialize, Deserialize, Debug)]
54pub struct NetworkState {
55    /// Network id for the current connection (or attempt).
56    pub id: Option<NetworkIdentifier>,
57    /// Current state for the connection.
58    pub state: Option<ConnectionState>,
59    /// Extra information for debugging or Settings display
60    pub status: Option<DisconnectStatus>,
61}
62
63#[derive(Serialize, Deserialize, Debug)]
64pub struct ClientStateSummary {
65    /// State indicating whether wlan will attempt to connect to networks or not.
66    pub state: Option<WlanClientState>,
67
68    /// Active connections, connection attempts or failed connections.
69    pub networks: Option<Vec<NetworkState>>,
70}
71
72#[derive(Serialize, Deserialize)]
73#[serde(remote = "fidl_sme::Protection")]
74pub(crate) enum ProtectionDef {
75    Unknown = 0,
76    Open = 1,
77    Wep = 2,
78    Wpa1 = 3,
79    Wpa1Wpa2PersonalTkipOnly = 4,
80    Wpa2PersonalTkipOnly = 5,
81    Wpa1Wpa2Personal = 6,
82    Wpa2Personal = 7,
83    Wpa2Wpa3Personal = 8,
84    Wpa3Personal = 9,
85    Wpa2Enterprise = 10,
86    Wpa3Enterprise = 11,
87}
88
89// The following definitions derive Serialize and Deserialize for remote types, i.e. types
90// defined in other crates. See https://serde.rs/remote-derive.html for more info.
91#[derive(Serialize, Deserialize)]
92#[repr(u32)]
93pub(crate) enum ChannelBandwidthDef {
94    Cbw20 = 0,
95    Cbw40 = 1,
96    Cbw40Below = 2,
97    Cbw80 = 3,
98    Cbw160 = 4,
99    Cbw80P80 = 5,
100    Unknown = u32::MAX,
101}
102
103impl From<fidl_ieee80211::ChannelBandwidth> for ChannelBandwidthDef {
104    fn from(fidl_type: fidl_ieee80211::ChannelBandwidth) -> Self {
105        match fidl_type {
106            fidl_ieee80211::ChannelBandwidth::Cbw20 => Self::Cbw20,
107            fidl_ieee80211::ChannelBandwidth::Cbw40 => Self::Cbw40,
108            fidl_ieee80211::ChannelBandwidth::Cbw40Below => Self::Cbw40Below,
109            fidl_ieee80211::ChannelBandwidth::Cbw80 => Self::Cbw80,
110            fidl_ieee80211::ChannelBandwidth::Cbw160 => Self::Cbw160,
111            fidl_ieee80211::ChannelBandwidth::Cbw80P80 => Self::Cbw80P80,
112            fidl_ieee80211::ChannelBandwidthUnknown!() => Self::Unknown,
113        }
114    }
115}
116
117impl From<ChannelBandwidthDef> for fidl_ieee80211::ChannelBandwidth {
118    fn from(serde_type: ChannelBandwidthDef) -> Self {
119        match serde_type {
120            ChannelBandwidthDef::Cbw20 => Self::Cbw20,
121            ChannelBandwidthDef::Cbw40 => Self::Cbw40,
122            ChannelBandwidthDef::Cbw40Below => Self::Cbw40Below,
123            ChannelBandwidthDef::Cbw80 => Self::Cbw80,
124            ChannelBandwidthDef::Cbw160 => Self::Cbw160,
125            ChannelBandwidthDef::Cbw80P80 => Self::Cbw80P80,
126            ChannelBandwidthDef::Unknown => Self::unknown(),
127        }
128    }
129}
130
131#[derive(Serialize, Deserialize)]
132pub(crate) struct WlanChannelDef {
133    pub primary: u8,
134    pub cbw: ChannelBandwidthDef,
135    pub secondary80: u8,
136}
137
138impl From<fidl_ieee80211::WlanChannel> for WlanChannelDef {
139    fn from(fidl_type: fidl_ieee80211::WlanChannel) -> Self {
140        Self {
141            primary: fidl_type.primary,
142            cbw: fidl_type.cbw.into(),
143            secondary80: fidl_type.secondary80,
144        }
145    }
146}
147
148impl From<WlanChannelDef> for fidl_ieee80211::WlanChannel {
149    fn from(serde_type: WlanChannelDef) -> Self {
150        Self {
151            primary: serde_type.primary,
152            cbw: serde_type.cbw.into(),
153            secondary80: serde_type.secondary80,
154        }
155    }
156}
157
158// Since fidl_common::BssType is a flexible enum, the generated Rust enum is declared
159// non-exhaustive, requiring a wildcard arm on all match expressions.
160//
161// However, deriving Serialize/Deserialize on a non-exhaustive enum generates code that have match
162// expressions that do not have wildcard arms. As a workaround, define the types used by serde
163// separately from the FIDL type and implement conversion functions between the two types.
164#[derive(Serialize, Deserialize)]
165pub(crate) enum BssTypeDef {
166    Infrastructure = 1,
167    Personal = 2,
168    Independent = 3,
169    Mesh = 4,
170    Unknown = 255,
171}
172
173impl From<BssTypeDef> for fidl_common::BssType {
174    fn from(serde_type: BssTypeDef) -> Self {
175        match serde_type {
176            BssTypeDef::Infrastructure => Self::Infrastructure,
177            BssTypeDef::Personal => Self::Personal,
178            BssTypeDef::Independent => Self::Independent,
179            BssTypeDef::Mesh => Self::Mesh,
180            BssTypeDef::Unknown => Self::unknown(),
181        }
182    }
183}
184
185impl From<fidl_common::BssType> for BssTypeDef {
186    fn from(fidl_type: fidl_common::BssType) -> Self {
187        match fidl_type {
188            fidl_common::BssType::Infrastructure => Self::Infrastructure,
189            fidl_common::BssType::Personal => Self::Personal,
190            fidl_common::BssType::Independent => Self::Independent,
191            fidl_common::BssType::Mesh => Self::Mesh,
192            _ => Self::Unknown,
193        }
194    }
195}
196
197#[derive(Serialize, Deserialize)]
198pub(crate) struct BssDescriptionDef {
199    pub bssid: [u8; 6],
200    pub bss_type: BssTypeDef,
201    pub beacon_period: u16,
202    pub capability_info: u16,
203    pub ies: Vec<u8>,
204    pub channel: WlanChannelDef,
205    pub rssi_dbm: i8,
206    pub snr_db: i8,
207}
208
209impl From<BssDescriptionDef> for fidl_common::BssDescription {
210    fn from(serde_type: BssDescriptionDef) -> Self {
211        Self {
212            bssid: serde_type.bssid,
213            bss_type: serde_type.bss_type.into(),
214            beacon_period: serde_type.beacon_period,
215            capability_info: serde_type.capability_info,
216            ies: serde_type.ies,
217            channel: serde_type.channel.into(),
218            rssi_dbm: serde_type.rssi_dbm,
219            snr_db: serde_type.snr_db,
220        }
221    }
222}
223
224impl From<fidl_common::BssDescription> for BssDescriptionDef {
225    fn from(fidl_type: fidl_common::BssDescription) -> Self {
226        Self {
227            bssid: fidl_type.bssid,
228            bss_type: fidl_type.bss_type.into(),
229            beacon_period: fidl_type.beacon_period,
230            capability_info: fidl_type.capability_info,
231            ies: fidl_type.ies,
232            channel: fidl_type.channel.into(),
233            rssi_dbm: fidl_type.rssi_dbm,
234            snr_db: fidl_type.snr_db,
235        }
236    }
237}
238
239#[derive(Serialize)]
240pub(crate) struct ServingApInfoDef {
241    pub bssid: [u8; 6],
242    pub ssid: Vec<u8>,
243    pub rssi_dbm: i8,
244    pub snr_db: i8,
245    pub channel: WlanChannelDef,
246    #[serde(with = "ProtectionDef")]
247    pub protection: fidl_sme::Protection,
248}
249
250impl From<fidl_sme::ServingApInfo> for ServingApInfoDef {
251    fn from(fidl_type: fidl_sme::ServingApInfo) -> Self {
252        Self {
253            bssid: fidl_type.bssid,
254            ssid: fidl_type.ssid,
255            rssi_dbm: fidl_type.rssi_dbm,
256            snr_db: fidl_type.snr_db,
257            channel: fidl_type.channel.into(),
258            protection: fidl_type.protection,
259        }
260    }
261}
262
263#[derive(Serialize)]
264#[serde(remote = "fidl_sme::Empty")]
265pub(crate) struct SmeEmptyDef;
266
267#[derive(Serialize)]
268pub(crate) enum ClientStatusResponseDef {
269    Connected(ServingApInfoDef),
270    Connecting(Vec<u8>),
271    Roaming(Vec<u8>),
272    #[serde(with = "SmeEmptyDef")]
273    Idle(fidl_sme::Empty),
274}
275
276impl From<fidl_sme::ClientStatusResponse> for ClientStatusResponseDef {
277    fn from(fidl_type: fidl_sme::ClientStatusResponse) -> Self {
278        match fidl_type {
279            fidl_sme::ClientStatusResponse::Connected(info) => Self::Connected(info.into()),
280            fidl_sme::ClientStatusResponse::Connecting(vec) => Self::Connecting(vec),
281            fidl_sme::ClientStatusResponse::Roaming(bssid) => Self::Roaming(bssid.to_vec()),
282            fidl_sme::ClientStatusResponse::Idle(empty) => Self::Idle(empty),
283        }
284    }
285}
286
287#[derive(Serialize)]
288pub(crate) enum WlanMacRoleDef {
289    Client = 1,
290    Ap = 2,
291    Mesh = 3,
292    Unknown = 255,
293}
294
295impl From<fidl_fuchsia_wlan_common::WlanMacRole> for WlanMacRoleDef {
296    fn from(fidl_type: fidl_fuchsia_wlan_common::WlanMacRole) -> Self {
297        match fidl_type {
298            fidl_fuchsia_wlan_common::WlanMacRole::Client => Self::Client,
299            fidl_fuchsia_wlan_common::WlanMacRole::Ap => Self::Ap,
300            fidl_fuchsia_wlan_common::WlanMacRole::Mesh => Self::Mesh,
301            fidl_fuchsia_wlan_common::WlanMacRoleUnknown!() => Self::Unknown,
302        }
303    }
304}
305
306#[derive(Serialize)]
307pub(crate) struct QueryIfaceResponseDef {
308    pub role: WlanMacRoleDef,
309    pub id: u16,
310    pub phy_id: u16,
311    pub phy_assigned_id: u16,
312    pub sta_addr: [u8; 6],
313}
314
315#[derive(Serialize)]
316pub(crate) struct QueryIfaceResponseWrapper(pub QueryIfaceResponseDef);
317
318impl From<fidl_fuchsia_wlan_device_service::QueryIfaceResponse> for QueryIfaceResponseDef {
319    fn from(resp: fidl_fuchsia_wlan_device_service::QueryIfaceResponse) -> QueryIfaceResponseDef {
320        QueryIfaceResponseDef {
321            role: resp.role.into(),
322            id: resp.id,
323            phy_id: resp.phy_id,
324            phy_assigned_id: resp.phy_assigned_id,
325            sta_addr: resp.sta_addr,
326        }
327    }
328}