sl4f_lib/wlan_policy/
ap_facade.rs

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2020 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.

use crate::wlan_policy::types::AccessPointState;
use anyhow::Error;
use fidl::endpoints::{create_endpoints, create_proxy};
use fidl_fuchsia_wlan_policy::{
    AccessPointControllerMarker, AccessPointControllerProxy, AccessPointListenerMarker,
    AccessPointProviderMarker, AccessPointStateUpdatesMarker, AccessPointStateUpdatesRequestStream,
    ConnectivityMode, Credential, NetworkConfig, NetworkIdentifier, OperatingBand, OperatingState,
    RequestStatus, SecurityType,
};
use fuchsia_component::client::connect_to_protocol;
use futures::TryStreamExt;
use std::cell::Cell;
use std::fmt::{self, Debug};

pub struct WlanApPolicyFacade {
    ap_controller: AccessPointControllerProxy,
    update_listener: Cell<Option<AccessPointStateUpdatesRequestStream>>,
}

impl Debug for WlanApPolicyFacade {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let listener = self.update_listener.take();
        let update_listener =
            if listener.is_some() { "Some(AccessPointStateUpdatesRequestStream)" } else { "None" }
                .to_string();
        self.update_listener.set(listener);

        f.debug_struct("WlanApPolicyFacade")
            .field("controller", &self.ap_controller)
            .field("update_listener", &update_listener)
            .finish()
    }
}

impl WlanApPolicyFacade {
    pub fn new() -> Result<WlanApPolicyFacade, Error> {
        let policy_provider = connect_to_protocol::<AccessPointProviderMarker>()?;
        let (ap_controller, server_end) = create_proxy::<AccessPointControllerMarker>();

        let (update_client_end, update_listener) =
            create_endpoints::<AccessPointStateUpdatesMarker>();
        let () = policy_provider.get_controller(server_end, update_client_end)?;
        let update_stream = update_listener.into_stream();
        Ok(WlanApPolicyFacade { ap_controller, update_listener: Cell::new(Some(update_stream)) })
    }

    pub async fn start_access_point(
        &self,
        target_ssid: Vec<u8>,
        type_: SecurityType,
        credential: Credential,
        mode: ConnectivityMode,
        band: OperatingBand,
    ) -> Result<(), Error> {
        let listener = connect_to_protocol::<AccessPointListenerMarker>()?;
        let (client_end, server_end) = create_endpoints::<AccessPointStateUpdatesMarker>();
        listener.get_listener(client_end)?;
        let mut server_stream = server_end.into_stream();

        match server_stream.try_next().await? {
            Some(update) => {
                let update = update.into_on_access_point_state_update();
                let (_, responder) = match update {
                    Some((update, responder)) => (update, responder),
                    None => return Err(format_err!("AP provider produced invalid update.")),
                };
                let _ = responder.send();
            }
            None => return Err(format_err!("initial steam already busted")),
        }

        let network_id = NetworkIdentifier { ssid: target_ssid.clone(), type_: type_ };

        match self
            .ap_controller
            .start_access_point(
                &NetworkConfig {
                    id: Some(network_id),
                    credential: Some(credential),
                    ..Default::default()
                },
                mode,
                band,
            )
            .await?
        {
            RequestStatus::Acknowledged => {}
            RequestStatus::RejectedNotSupported => {
                return Err(format_err!("failed to start AP (not supported)"))
            }
            RequestStatus::RejectedIncompatibleMode => {
                return Err(format_err!("failed to start AP (incompatible mode)"))
            }
            RequestStatus::RejectedAlreadyInUse => {
                return Err(format_err!("failed to start AP (already in use)"))
            }
            RequestStatus::RejectedDuplicateRequest => {
                return Err(format_err!("failed to start AP (duplicate request)"))
            }
        }

        while let Some(update_request) = server_stream.try_next().await.unwrap() {
            let update = update_request.into_on_access_point_state_update();
            let (updates, responder) = match update {
                Some((update, responder)) => (update, responder),
                None => return Err(format_err!("AP provider produced invalid update.")),
            };
            let _ = responder.send();

            for update in updates {
                match update.state {
                    Some(state) => match state {
                        OperatingState::Failed => {
                            return Err(format_err!("Failed to start AP."));
                        }
                        OperatingState::Starting => {
                            continue;
                        }
                        OperatingState::Active => return Ok(()),
                    },
                    None => continue,
                }
            }
        }
        return Err(format_err!("AP update stream failed unexpectedly"));
    }

    pub async fn stop_access_point(
        &self,
        target_ssid: Vec<u8>,
        type_: SecurityType,
        credential: Credential,
    ) -> Result<(), Error> {
        let network_id = NetworkIdentifier { ssid: target_ssid.clone(), type_: type_ };
        match self
            .ap_controller
            .stop_access_point(&NetworkConfig {
                id: Some(network_id),
                credential: Some(credential),
                ..Default::default()
            })
            .await?
        {
            RequestStatus::Acknowledged => Ok(()),
            RequestStatus::RejectedNotSupported => {
                Err(format_err!("Failed to stop AP (not supported)"))
            }
            RequestStatus::RejectedIncompatibleMode => {
                Err(format_err!("Failed to stop AP (incompatible mode)"))
            }
            RequestStatus::RejectedAlreadyInUse => {
                Err(format_err!("Failed to stop AP (already in use)"))
            }
            RequestStatus::RejectedDuplicateRequest => {
                Err(format_err!("Failed to stop AP (duplicate request)"))
            }
        }
    }

    pub async fn stop_all_access_points(&self) -> Result<(), Error> {
        self.ap_controller.stop_all_access_points()?;
        Ok(())
    }

    /// Creates a listener update stream for getting status updates.
    fn init_listener() -> Result<AccessPointStateUpdatesRequestStream, Error> {
        let listener = connect_to_protocol::<AccessPointListenerMarker>()?;
        let (client_end, server_end) =
            fidl::endpoints::create_endpoints::<AccessPointStateUpdatesMarker>();
        listener.get_listener(client_end)?;
        Ok(server_end.into_stream())
    }

    /// This function will set a new listener even if there is one because new listeners will get
    /// the most recent update immediately without waiting.
    pub fn set_new_listener(&self) -> Result<(), Error> {
        self.update_listener.set(Some(Self::init_listener()?));
        Ok(())
    }

    /// Wait for and return an AP update. If this is the first update gotten from the facade
    /// since the AP controller or a new update listener has been created, it will get an
    /// immediate status. After that, it will wait for a change and return a status when there has
    /// been a change since the last call to get_update. This call will hang if there are no
    /// updates.
    /// This function is not thread safe, so there should not be multiple get_update calls at the
    /// same time unless a new listener is set between them. There is no lock around the
    /// update_listener field of the facade in order to prevent a hanging get_update from blocking
    /// all future get_updates.
    pub async fn get_update(&self) -> Result<Vec<AccessPointState>, Error> {
        // Initialize the update listener if it has not been initialized.
        let listener = self.update_listener.take();
        let mut update_listener = if listener.is_none() {
            Self::init_listener()
        } else {
            listener.ok_or_else(|| format_err!("failed to set update listener of facade"))
        }?;

        if let Some(update_request) = update_listener.try_next().await? {
            let update = update_request.into_on_access_point_state_update();
            let (update, responder) = match update {
                Some((update, responder)) => (update, responder),
                None => return Err(format_err!("Client provider produced invalid update.")).into(),
            };
            // Ack the update.
            responder.send().map_err(|e| format_err!("failed to ack update: {}", e))?;
            // Put the update listener back in the facade
            self.update_listener.set(Some(update_listener));

            let update = update.into_iter().map(|update| AccessPointState::from(update)).collect();
            Ok(update)
        } else {
            self.update_listener.set(Some(update_listener));
            Err(format_err!("update listener's next update is None"))
        }
    }
}