wlan_sme/
lib.rs

1// Copyright 2018 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
5// Turn on additional lints that could lead to unexpected crashes in production code
6#![warn(clippy::indexing_slicing)]
7#![cfg_attr(test, allow(clippy::indexing_slicing))]
8#![warn(clippy::unwrap_used)]
9#![cfg_attr(test, allow(clippy::unwrap_used))]
10#![warn(clippy::expect_used)]
11#![cfg_attr(test, allow(clippy::expect_used))]
12#![warn(clippy::unreachable)]
13#![cfg_attr(test, allow(clippy::unreachable))]
14#![warn(clippy::unimplemented)]
15#![cfg_attr(test, allow(clippy::unimplemented))]
16
17pub mod ap;
18pub mod client;
19pub mod serve;
20#[cfg(test)]
21pub mod test_utils;
22
23use fidl_fuchsia_wlan_mlme::{self as fidl_mlme, MlmeEvent};
24use futures::channel::mpsc;
25use thiserror::Error;
26use wlan_common::sink::UnboundedSink;
27use wlan_common::timer;
28use {
29    fidl_fuchsia_wlan_common as fidl_common, fidl_fuchsia_wlan_ieee80211 as fidl_ieee80211,
30    fidl_fuchsia_wlan_stats as fidl_stats,
31};
32
33#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
34pub struct Config {
35    pub wep_supported: bool,
36    pub wpa1_supported: bool,
37}
38
39impl Config {
40    pub fn with_wep(mut self) -> Self {
41        self.wep_supported = true;
42        self
43    }
44
45    pub fn with_wpa1(mut self) -> Self {
46        self.wpa1_supported = true;
47        self
48    }
49}
50
51#[derive(Debug)]
52pub enum MlmeRequest {
53    Scan(fidl_mlme::ScanRequest),
54    AuthResponse(fidl_mlme::AuthenticateResponse),
55    AssocResponse(fidl_mlme::AssociateResponse),
56    Connect(fidl_mlme::ConnectRequest),
57    Reconnect(fidl_mlme::ReconnectRequest),
58    Roam(fidl_mlme::RoamRequest),
59    Deauthenticate(fidl_mlme::DeauthenticateRequest),
60    Disassociate(fidl_mlme::DisassociateRequest),
61    Eapol(fidl_mlme::EapolRequest),
62    SetKeys(fidl_mlme::SetKeysRequest),
63    SetCtrlPort(fidl_mlme::SetControlledPortRequest),
64    Start(fidl_mlme::StartRequest),
65    Stop(fidl_mlme::StopRequest),
66    GetIfaceStats(responder::Responder<fidl_mlme::GetIfaceStatsResponse>),
67    GetIfaceHistogramStats(responder::Responder<fidl_mlme::GetIfaceHistogramStatsResponse>),
68    GetSignalReport(responder::Responder<Result<fidl_stats::SignalReport, i32>>),
69    ListMinstrelPeers(responder::Responder<fidl_mlme::MinstrelListResponse>),
70    GetMinstrelStats(
71        fidl_mlme::MinstrelStatsRequest,
72        responder::Responder<fidl_mlme::MinstrelStatsResponse>,
73    ),
74    SaeHandshakeResp(fidl_mlme::SaeHandshakeResponse),
75    SaeFrameTx(fidl_mlme::SaeFrame),
76    WmmStatusReq,
77    FinalizeAssociation(fidl_mlme::NegotiatedCapabilities),
78    QueryDeviceInfo(responder::Responder<fidl_mlme::DeviceInfo>),
79    QueryMacSublayerSupport(responder::Responder<fidl_common::MacSublayerSupport>),
80    QuerySecuritySupport(responder::Responder<fidl_common::SecuritySupport>),
81    QuerySpectrumManagementSupport(responder::Responder<fidl_common::SpectrumManagementSupport>),
82    QueryTelemetrySupport(responder::Responder<Result<fidl_stats::TelemetrySupport, i32>>),
83    SetMacAddress(fidl_ieee80211::MacAddr, responder::Responder<Result<(), i32>>),
84}
85
86impl MlmeRequest {
87    pub fn name(&self) -> &'static str {
88        match self {
89            Self::Scan(_) => "Scan",
90            Self::AuthResponse(_) => "AuthResponse",
91            Self::AssocResponse(_) => "AssocResponse",
92            Self::Connect(_) => "Connect",
93            Self::Reconnect(_) => "Reconnect",
94            Self::Roam(_) => "Roam",
95            Self::Deauthenticate(_) => "Deauthenticate",
96            Self::Disassociate(_) => "Disassociate",
97            Self::Eapol(_) => "Eapol",
98            Self::SetKeys(_) => "SetKeys",
99            Self::SetCtrlPort(_) => "SetCtrlPort",
100            Self::Start(_) => "Start",
101            Self::Stop(_) => "Stop",
102            Self::GetIfaceStats(_) => "GetIfaceStats",
103            Self::GetIfaceHistogramStats(_) => "GetIfaceHistogramStats",
104            Self::GetSignalReport(_) => "GetSignalReport",
105            Self::ListMinstrelPeers(_) => "ListMinstrelPeers",
106            Self::GetMinstrelStats(_, _) => "GetMinstrelStats",
107            Self::SaeHandshakeResp(_) => "SaeHandshakeResp",
108            Self::SaeFrameTx(_) => "SaeFrameTx",
109            Self::WmmStatusReq => "WmmStatusReq",
110            Self::FinalizeAssociation(_) => "FinalizeAssociation",
111            Self::QueryDeviceInfo(_) => "QueryDeviceInfo",
112            Self::QueryMacSublayerSupport(_) => "QueryMacSublayerSupport",
113            Self::QuerySecuritySupport(_) => "QuerySecuritySupport",
114            Self::QuerySpectrumManagementSupport(_) => "QuerySpectrumManagementSupport",
115            Self::QueryTelemetrySupport(_) => "QueryTelemetrySupport",
116            Self::SetMacAddress(..) => "SetMacAddress",
117        }
118    }
119}
120
121pub trait Station {
122    type Event;
123
124    fn on_mlme_event(&mut self, event: fidl_mlme::MlmeEvent);
125    fn on_timeout(&mut self, timed_event: timer::Event<Self::Event>);
126}
127
128pub type MlmeStream = mpsc::UnboundedReceiver<MlmeRequest>;
129pub type MlmeEventStream = mpsc::UnboundedReceiver<MlmeEvent>;
130pub type MlmeSink = UnboundedSink<MlmeRequest>;
131pub type MlmeEventSink = UnboundedSink<MlmeEvent>;
132
133pub mod responder {
134    use futures::channel::oneshot;
135
136    #[derive(Debug)]
137    pub struct Responder<T>(oneshot::Sender<T>);
138
139    impl<T> Responder<T> {
140        pub fn new() -> (Self, oneshot::Receiver<T>) {
141            let (sender, receiver) = oneshot::channel();
142            (Responder(sender), receiver)
143        }
144
145        pub fn respond(self, result: T) {
146            #[allow(
147                clippy::unnecessary_lazy_evaluations,
148                reason = "mass allow for https://fxbug.dev/381896734"
149            )]
150            self.0.send(result).unwrap_or_else(|_| ());
151        }
152    }
153}
154
155/// Safely log MlmeEvents without printing private information.
156fn mlme_event_name(event: &MlmeEvent) -> &str {
157    match event {
158        MlmeEvent::OnScanResult { .. } => "OnScanResult",
159        MlmeEvent::OnScanEnd { .. } => "OnScanEnd",
160        MlmeEvent::ConnectConf { .. } => "ConnectConf",
161        MlmeEvent::RoamConf { .. } => "RoamConf",
162        MlmeEvent::RoamStartInd { .. } => "RoamStartInd",
163        MlmeEvent::RoamResultInd { .. } => "RoamResultInd",
164        MlmeEvent::AuthenticateInd { .. } => "AuthenticateInd",
165        MlmeEvent::DeauthenticateConf { .. } => "DeauthenticateConf",
166        MlmeEvent::DeauthenticateInd { .. } => "DeauthenticateInd",
167        MlmeEvent::AssociateInd { .. } => "AssociateInd",
168        MlmeEvent::DisassociateConf { .. } => "DisassociateConf",
169        MlmeEvent::DisassociateInd { .. } => "DisassociateInd",
170        MlmeEvent::SetKeysConf { .. } => "SetKeysConf",
171        MlmeEvent::StartConf { .. } => "StartConf",
172        MlmeEvent::StopConf { .. } => "StopConf",
173        MlmeEvent::EapolConf { .. } => "EapolConf",
174        MlmeEvent::SignalReport { .. } => "SignalReport",
175        MlmeEvent::EapolInd { .. } => "EapolInd",
176        MlmeEvent::RelayCapturedFrame { .. } => "RelayCapturedFrame",
177        MlmeEvent::OnChannelSwitched { .. } => "OnChannelSwitched",
178        MlmeEvent::OnPmkAvailable { .. } => "OnPmkAvailable",
179        MlmeEvent::OnSaeHandshakeInd { .. } => "OnSaeHandshakeInd",
180        MlmeEvent::OnSaeFrameRx { .. } => "OnSaeFrameRx",
181        MlmeEvent::OnWmmStatusResp { .. } => "OnWmmStatusResp",
182    }
183}
184
185#[derive(Debug, Error)]
186pub enum Error {
187    #[error("scan end while not scanning")]
188    ScanEndNotScanning,
189    #[error("scan end with wrong txn id")]
190    ScanEndWrongTxnId,
191    #[error("scan result while not scanning")]
192    ScanResultNotScanning,
193    #[error("scan result with wrong txn id")]
194    ScanResultWrongTxnId,
195}