1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

pub mod ap;
pub mod client;
pub mod serve;
#[cfg(test)]
pub mod test_utils;

use {
    fidl_fuchsia_wlan_common as fidl_common,
    fidl_fuchsia_wlan_mlme::{self as fidl_mlme, MlmeEvent},
    futures::channel::mpsc,
    thiserror::Error,
    wlan_common::{sink::UnboundedSink, timer},
};

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
pub struct Config {
    pub wep_supported: bool,
    pub wpa1_supported: bool,
}

impl Config {
    pub fn with_wep(mut self) -> Self {
        self.wep_supported = true;
        self
    }

    pub fn with_wpa1(mut self) -> Self {
        self.wpa1_supported = true;
        self
    }
}

#[derive(Debug)]
pub enum MlmeRequest {
    Scan(fidl_mlme::ScanRequest),
    AuthResponse(fidl_mlme::AuthenticateResponse),
    AssocResponse(fidl_mlme::AssociateResponse),
    Connect(fidl_mlme::ConnectRequest),
    Reconnect(fidl_mlme::ReconnectRequest),
    Deauthenticate(fidl_mlme::DeauthenticateRequest),
    Disassociate(fidl_mlme::DisassociateRequest),
    Eapol(fidl_mlme::EapolRequest),
    SetKeys(fidl_mlme::SetKeysRequest),
    DeleteKeys(fidl_mlme::DeleteKeysRequest),
    SetCtrlPort(fidl_mlme::SetControlledPortRequest),
    Reset(fidl_mlme::ResetRequest),
    Start(fidl_mlme::StartRequest),
    Stop(fidl_mlme::StopRequest),
    GetIfaceCounterStats(responder::Responder<fidl_mlme::GetIfaceCounterStatsResponse>),
    GetIfaceHistogramStats(responder::Responder<fidl_mlme::GetIfaceHistogramStatsResponse>),
    ListMinstrelPeers(responder::Responder<fidl_mlme::MinstrelListResponse>),
    GetMinstrelStats(
        fidl_mlme::MinstrelStatsRequest,
        responder::Responder<fidl_mlme::MinstrelStatsResponse>,
    ),
    SaeHandshakeResp(fidl_mlme::SaeHandshakeResponse),
    SaeFrameTx(fidl_mlme::SaeFrame),
    WmmStatusReq,
    FinalizeAssociation(fidl_mlme::NegotiatedCapabilities),
    QueryDeviceInfo(responder::Responder<fidl_mlme::DeviceInfo>),
    QueryDiscoverySupport(responder::Responder<fidl_common::DiscoverySupport>),
    QueryMacSublayerSupport(responder::Responder<fidl_common::MacSublayerSupport>),
    QuerySecuritySupport(responder::Responder<fidl_common::SecuritySupport>),
    QuerySpectrumManagementSupport(responder::Responder<fidl_common::SpectrumManagementSupport>),
}

impl MlmeRequest {
    pub fn name(&self) -> &'static str {
        match self {
            Self::Scan(_) => "Scan",
            Self::AuthResponse(_) => "AuthResponse",
            Self::AssocResponse(_) => "AssocResponse",
            Self::Connect(_) => "Connect",
            Self::Reconnect(_) => "Reconnect",
            Self::Deauthenticate(_) => "Deauthenticate",
            Self::Disassociate(_) => "Disassociate",
            Self::Eapol(_) => "Eapol",
            Self::SetKeys(_) => "SetKeys",
            Self::DeleteKeys(_) => "DeleteKeys",
            Self::SetCtrlPort(_) => "SetCtrlPort",
            Self::Reset(_) => "Reset",
            Self::Start(_) => "Start",
            Self::Stop(_) => "Stop",
            Self::GetIfaceCounterStats(_) => "GetIfaceCounterStats",
            Self::GetIfaceHistogramStats(_) => "GetIfaceHistogramStats",
            Self::ListMinstrelPeers(_) => "ListMinstrelPeers",
            Self::GetMinstrelStats(_, _) => "GetMinstrelStats",
            Self::SaeHandshakeResp(_) => "SaeHandshakeResp",
            Self::SaeFrameTx(_) => "SaeFrameTx",
            Self::WmmStatusReq => "WmmStatusReq",
            Self::FinalizeAssociation(_) => "FinalizeAssociation",
            Self::QueryDeviceInfo(_) => "QueryDeviceInfo",
            Self::QueryDiscoverySupport(_) => "QueryDiscoverySupport",
            Self::QueryMacSublayerSupport(_) => "QueryMacSublayerSupport",
            Self::QuerySecuritySupport(_) => "QuerySecuritySupport",
            Self::QuerySpectrumManagementSupport(_) => "QuerySpectrumManagementSupport",
        }
    }
}

pub trait Station {
    type Event;

    fn on_mlme_event(&mut self, event: fidl_mlme::MlmeEvent);
    fn on_timeout(&mut self, timed_event: timer::Event<Self::Event>);
}

pub type MlmeStream = mpsc::UnboundedReceiver<MlmeRequest>;
pub type MlmeEventStream = mpsc::UnboundedReceiver<MlmeEvent>;
pub type MlmeSink = UnboundedSink<MlmeRequest>;
pub type MlmeEventSink = UnboundedSink<MlmeEvent>;

pub mod responder {
    use futures::channel::oneshot;

    #[derive(Debug)]
    pub struct Responder<T>(oneshot::Sender<T>);

    impl<T> Responder<T> {
        pub fn new() -> (Self, oneshot::Receiver<T>) {
            let (sender, receiver) = oneshot::channel();
            (Responder(sender), receiver)
        }

        pub fn respond(self, result: T) {
            self.0.send(result).unwrap_or_else(|_| ());
        }
    }
}

/// Safely log MlmeEvents without printing private information.
fn mlme_event_name(event: &MlmeEvent) -> &str {
    match event {
        MlmeEvent::OnScanResult { .. } => "OnScanResult",
        MlmeEvent::OnScanEnd { .. } => "OnScanEnd",
        MlmeEvent::ConnectConf { .. } => "ConnectConf",
        MlmeEvent::RoamConf { .. } => "RoamConf",
        MlmeEvent::AuthenticateInd { .. } => "AuthenticateInd",
        MlmeEvent::DeauthenticateConf { .. } => "DeauthenticateConf",
        MlmeEvent::DeauthenticateInd { .. } => "DeauthenticateInd",
        MlmeEvent::AssociateInd { .. } => "AssociateInd",
        MlmeEvent::DisassociateConf { .. } => "DisassociateConf",
        MlmeEvent::DisassociateInd { .. } => "DisassociateInd",
        MlmeEvent::SetKeysConf { .. } => "SetKeysConf",
        MlmeEvent::StartConf { .. } => "StartConf",
        MlmeEvent::StopConf { .. } => "StopConf",
        MlmeEvent::EapolConf { .. } => "EapolConf",
        MlmeEvent::SignalReport { .. } => "SignalReport",
        MlmeEvent::EapolInd { .. } => "EapolInd",
        MlmeEvent::RelayCapturedFrame { .. } => "RelayCapturedFrame",
        MlmeEvent::OnChannelSwitched { .. } => "OnChannelSwitched",
        MlmeEvent::OnPmkAvailable { .. } => "OnPmkAvailable",
        MlmeEvent::OnSaeHandshakeInd { .. } => "OnSaeHandshakeInd",
        MlmeEvent::OnSaeFrameRx { .. } => "OnSaeFrameRx",
        MlmeEvent::OnWmmStatusResp { .. } => "OnWmmStatusResp",
    }
}

#[derive(Debug, Error)]
pub enum Error {
    #[error("scan end while not scanning")]
    ScanEndNotScanning,
    #[error("scan end with wrong txn id")]
    ScanEndWrongTxnId,
    #[error("scan result while not scanning")]
    ScanResultNotScanning,
    #[error("scan result with wrong txn id")]
    ScanResultWrongTxnId,
}