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