1use crate::{MlmeEventStream, MlmeSink, MlmeStream, ap as ap_sme};
6use fuchsia_sync::Mutex;
7use futures::channel::mpsc;
8use futures::prelude::*;
9use futures::select;
10use ieee80211::Ssid;
11use log::error;
12use std::pin::pin;
13use std::sync::Arc;
14use wlan_common::RadioConfig;
15use {
16 fidl_fuchsia_wlan_common as fidl_common, fidl_fuchsia_wlan_mlme as fidl_mlme,
17 fidl_fuchsia_wlan_sme as fidl_sme,
18};
19
20pub type Endpoint = fidl::endpoints::ServerEnd<fidl_sme::ApSmeMarker>;
21type Sme = ap_sme::ApSme;
22
23pub fn serve(
24 device_info: fidl_mlme::DeviceInfo,
25 spectrum_management_support: fidl_common::SpectrumManagementSupport,
26 event_stream: MlmeEventStream,
27 new_fidl_clients: mpsc::UnboundedReceiver<Endpoint>,
28) -> (MlmeSink, MlmeStream, impl Future<Output = Result<(), anyhow::Error>>) {
29 let (sme, mlme_sink, mlme_stream, time_stream) =
30 Sme::new(device_info, spectrum_management_support);
31 let fut = async move {
32 let sme = Arc::new(Mutex::new(sme));
33 let mlme_sme = super::serve_mlme_sme(event_stream, Arc::clone(&sme), time_stream);
34 let sme_fidl = super::serve_fidl(&*sme, new_fidl_clients, handle_fidl_request);
35 let mlme_sme = pin!(mlme_sme);
36 let sme_fidl = pin!(sme_fidl);
37 select! {
38 mlme_sme = mlme_sme.fuse() => mlme_sme?,
39 sme_fidl = sme_fidl.fuse() => match sme_fidl? {},
40 }
41 Ok(())
42 };
43 (mlme_sink, mlme_stream, fut)
44}
45
46async fn handle_fidl_request(
47 sme: &Mutex<Sme>,
48 request: fidl_sme::ApSmeRequest,
49) -> Result<(), ::fidl::Error> {
50 match request {
51 fidl_sme::ApSmeRequest::Start { config, responder } => {
52 let r = start(sme, config).await;
53 responder.send(r)?;
54 }
55 fidl_sme::ApSmeRequest::Stop { responder } => {
56 let r = stop(sme).await;
57 responder.send(r)?;
58 }
59 fidl_sme::ApSmeRequest::Status { responder } => {
60 let r = status(sme);
61 responder.send(&r)?;
62 }
63 }
64 Ok(())
65}
66
67async fn start(sme: &Mutex<Sme>, config: fidl_sme::ApConfig) -> fidl_sme::StartApResultCode {
68 let radio_cfg = match RadioConfig::try_from(config.radio_cfg) {
69 Ok(radio_cfg) => radio_cfg,
70 Err(e) => {
71 error!("Could not convert RadioConfig from ApConfig: {:?}", e);
72 return fidl_sme::StartApResultCode::InternalError;
73 }
74 };
75
76 let sme_config = ap_sme::Config {
77 ssid: Ssid::from_bytes_unchecked(config.ssid),
78 password: config.password,
79 radio_cfg,
80 };
81
82 let receiver = sme.lock().on_start_command(sme_config);
83 let r = receiver.await.unwrap_or_else(|_| {
84 error!("Responder for AP Start command was dropped without sending a response");
85 ap_sme::StartResult::InternalError
86 });
87
88 match r {
89 ap_sme::StartResult::Success => fidl_sme::StartApResultCode::Success,
90 ap_sme::StartResult::AlreadyStarted => fidl_sme::StartApResultCode::AlreadyStarted,
91 ap_sme::StartResult::InternalError => fidl_sme::StartApResultCode::InternalError,
92 ap_sme::StartResult::Canceled => fidl_sme::StartApResultCode::Canceled,
93 ap_sme::StartResult::TimedOut => fidl_sme::StartApResultCode::TimedOut,
94 ap_sme::StartResult::PreviousStartInProgress => {
95 fidl_sme::StartApResultCode::PreviousStartInProgress
96 }
97 ap_sme::StartResult::InvalidArguments(e) => {
98 error!("Invalid arguments for AP start: {}", e);
99 fidl_sme::StartApResultCode::InvalidArguments
100 }
101 }
102}
103
104async fn stop(sme: &Mutex<Sme>) -> fidl_sme::StopApResultCode {
105 let receiver = sme.lock().on_stop_command();
106 receiver.await.unwrap_or_else(|_| {
107 error!("Responder for AP Stop command was dropped without sending a response");
108 fidl_sme::StopApResultCode::InternalError
109 })
110}
111
112fn status(sme: &Mutex<Sme>) -> fidl_sme::ApStatusResponse {
113 fidl_sme::ApStatusResponse { running_ap: sme.lock().get_running_ap().map(Box::new) }
114}