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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2023 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.

//! Event handlers that filter their input events to more specific events.
//!
//! Filters are powered by the `AsEvent` trait, which extracts a reference to an event sub-type
//! from a super-type (typically a sum type like `enum`s). The handlers composed by filters are
//! only invoked when such a sub-type is received. Otherwise, filters return
//! [`Handled::Unmatched`].
//!
//! [`Handled::Unmatched`]: crate::event::Handled

use fidl_fuchsia_wlan_tap as fidl_tap;

use crate::event::{Handled, Handler, StartMacArgs};

// This event has no fields and is zero-size. This constant is used to return a static reference in
// the corresponding `AsEvent` implementation.
const START_MAC_ARGS: StartMacArgs = StartMacArgs;

pub trait AsEvent<E> {
    fn as_event(&self) -> Option<&E>;
}

impl<E> AsEvent<E> for E {
    fn as_event(&self) -> Option<&E> {
        Some(self)
    }
}

impl AsEvent<fidl_tap::JoinBssArgs> for fidl_tap::WlantapPhyEvent {
    fn as_event(&self) -> Option<&fidl_tap::JoinBssArgs> {
        match self {
            fidl_tap::WlantapPhyEvent::JoinBss { ref args } => Some(args),
            _ => None,
        }
    }
}

impl AsEvent<fidl_tap::StartScanArgs> for fidl_tap::WlantapPhyEvent {
    fn as_event(&self) -> Option<&fidl_tap::StartScanArgs> {
        match self {
            fidl_tap::WlantapPhyEvent::StartScan { ref args } => Some(args),
            _ => None,
        }
    }
}

impl AsEvent<fidl_tap::SetChannelArgs> for fidl_tap::WlantapPhyEvent {
    fn as_event(&self) -> Option<&fidl_tap::SetChannelArgs> {
        match self {
            fidl_tap::WlantapPhyEvent::SetChannel { ref args } => Some(args),
            _ => None,
        }
    }
}

impl AsEvent<fidl_tap::SetCountryArgs> for fidl_tap::WlantapPhyEvent {
    fn as_event(&self) -> Option<&fidl_tap::SetCountryArgs> {
        match self {
            fidl_tap::WlantapPhyEvent::SetCountry { ref args } => Some(args),
            _ => None,
        }
    }
}

impl AsEvent<StartMacArgs> for fidl_tap::WlantapPhyEvent {
    fn as_event(&self) -> Option<&StartMacArgs> {
        match self {
            fidl_tap::WlantapPhyEvent::WlanSoftmacStart { .. } => Some(&START_MAC_ARGS),
            _ => None,
        }
    }
}

impl AsEvent<fidl_tap::TxArgs> for fidl_tap::WlantapPhyEvent {
    fn as_event(&self) -> Option<&fidl_tap::TxArgs> {
        match self {
            fidl_tap::WlantapPhyEvent::Tx { ref args } => Some(args),
            _ => None,
        }
    }
}

/// Filters [`WlantapPhyEvent`]s to [`JoinBssArgs`].
///
/// The composed event handler must accept [`JoinBssArgs`] and is only invoked when such an event
/// is received by the filter. Otherwise, the filter returns [`Handled::Unmatched`].
///
/// [`Handled::Unmatched`]: crate::event::Handled::Unmatched
/// [`JoinBssArgs`]: fidl_fuchsia_wlan_tap::JoinBssArgs
/// [`WlantapPhyEvent`]: fidl_fuchsia_wlan_tap::WlantapPhyEvent
pub fn on_join_bss<H, S, E>(handler: H) -> impl Handler<S, E, Output = H::Output>
where
    H: Handler<S, fidl_tap::JoinBssArgs>,
    E: AsEvent<fidl_tap::JoinBssArgs>,
{
    filter(handler)
}

/// Filters [`WlantapPhyEvent`]s to [`StartScanArgs`].
///
/// The composed event handler must accept [`StartScanArgs`] and is only invoked when such an event
/// is received by the filter. Otherwise, the filter returns [`Handled::Unmatched`].
///
/// [`Handled::Unmatched`]: crate::event::Handled::Unmatched
/// [`StartScanArgs`]: fidl_fuchsia_wlan_tap::StartScanArgs
/// [`WlantapPhyEvent`]: fidl_fuchsia_wlan_tap::WlantapPhyEvent
pub fn on_scan<H, S, E>(handler: H) -> impl Handler<S, E, Output = H::Output>
where
    H: Handler<S, fidl_tap::StartScanArgs>,
    E: AsEvent<fidl_tap::StartScanArgs>,
{
    filter(handler)
}

/// Filters [`WlantapPhyEvent`]s to [`SetChannelArgs`].
///
/// The composed event handler must accept [`SetChannelArgs`] and is only invoked when such an
/// event is received by the filter. Otherwise, the filter returns [`Handled::Unmatched`].
///
/// [`Handled::Unmatched`]: crate::event::Handled::Unmatched
/// [`SetChannelArgs`]: fidl_fuchsia_wlan_tap::SetChannelArgs
/// [`WlantapPhyEvent`]: fidl_fuchsia_wlan_tap::WlantapPhyEvent
pub fn on_set_channel<H, S, E>(handler: H) -> impl Handler<S, E, Output = H::Output>
where
    H: Handler<S, fidl_tap::SetChannelArgs>,
    E: AsEvent<fidl_tap::SetChannelArgs>,
{
    filter(handler)
}

/// Filters [`WlantapPhyEvent`]s to [`SetCountryArgs`].
///
/// The composed event handler must accept [`SetCountryArgs`] and is only invoked when such an
/// event is received by the filter. Otherwise, the filter returns [`Handled::Unmatched`].
///
/// [`Handled::Unmatched`]: crate::event::Handled::Unmatched
/// [`SetCountryArgs`]: fidl_fuchsia_wlan_tap::SetCountryArgs
/// [`WlantapPhyEvent`]: fidl_fuchsia_wlan_tap::WlantapPhyEvent
pub fn on_set_country<H, S, E>(handler: H) -> impl Handler<S, E, Output = H::Output>
where
    H: Handler<S, fidl_tap::SetCountryArgs>,
    E: AsEvent<fidl_tap::SetCountryArgs>,
{
    filter(handler)
}

/// Filters [`WlantapPhyEvent`]s to [`StartMacArgs`].
///
/// The composed event handler must accept [`StartMacArgs`] and is only invoked when such an
/// event is received by the filter. Otherwise, the filter returns [`Handled::Unmatched`].
///
/// [`Handled::Unmatched`]: crate::event::Handled::Unmatched
/// [`StartMacArgs`]: crate::event::StartMacArgs
/// [`WlantapPhyEvent`]: fidl_fuchsia_wlan_tap::WlantapPhyEvent
pub fn on_start_mac<H, S, E>(handler: H) -> impl Handler<S, E, Output = H::Output>
where
    H: Handler<S, StartMacArgs>,
    E: AsEvent<StartMacArgs>,
{
    filter(handler)
}

/// Filters [`WlantapPhyEvent`]s to [`TxArgs`].
///
/// The composed event handler must accept [`TxArgs`] and is only invoked when such an event is
/// received by the filter. Otherwise, the filter returns [`Handled::Unmatched`].
///
/// [`Handled::Unmatched`]: crate::event::Handled::Unmatched
/// [`TxArgs`]: fidl_fuchsia_wlan_tap::TxArgs
/// [`WlantapPhyEvent`]: fidl_fuchsia_wlan_tap::WlantapPhyEvent
pub fn on_transmit<H, S, E>(handler: H) -> impl Handler<S, E, Output = H::Output>
where
    H: Handler<S, fidl_tap::TxArgs>,
    E: AsEvent<fidl_tap::TxArgs>,
{
    filter(handler)
}

/// Filters `E1` events to more specific `E2` events.
///
/// It must be possible to extract a reference to an `E2` from an `E1` via `AsEvent` to use this
/// function. The composed event handler must accept `E2` events and is only invoked when the
/// filter receives an `E1` event from which an `E2` event can be retrieved. Otherwise, the filter
/// returns [`Handled::Unmatched`].
///
/// [`Handled::Unmatched`]: crate::event::Handled::Unmatched
fn filter<H, S, E1, E2>(mut handler: H) -> impl Handler<S, E1, Output = H::Output>
where
    H: Handler<S, E2>,
    E1: AsEvent<E2>,
{
    move |state: &mut S, event: &E1| match event.as_event() {
        Some(event) => handler.call(state, event),
        _ => Handled::Unmatched,
    }
}