drivers_only_common/
sme_helpers.rs

1// Copyright 2024 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//
5use lazy_static::lazy_static;
6use rand::distributions::Alphanumeric;
7use rand::Rng;
8use {
9    fidl_fuchsia_wlan_common as fidl_common, fidl_fuchsia_wlan_ieee80211 as fidl_ieee80211,
10    fidl_fuchsia_wlan_sme as fidl_sme,
11};
12
13lazy_static! {
14    pub static ref DEFAULT_OPEN_AP_CONFIG: fidl_sme::ApConfig = fidl_sme::ApConfig {
15        ssid: random_ssid(),
16        password: vec![],
17        radio_cfg: fidl_sme::RadioConfig {
18            phy: fidl_common::WlanPhyType::Ofdm,
19            channel: fidl_common::WlanChannel {
20                primary: 1,
21                cbw: fidl_common::ChannelBandwidth::Cbw20,
22                secondary80: 0,
23            },
24        },
25    };
26}
27
28pub async fn get_client_sme(
29    generic_sme_proxy: &fidl_sme::GenericSmeProxy,
30) -> fidl_sme::ClientSmeProxy {
31    let (client_sme_proxy, client_sme_server) = fidl::endpoints::create_proxy();
32    generic_sme_proxy
33        .get_client_sme(client_sme_server)
34        .await
35        .expect("FIDL error")
36        .expect("GetClientSme Error");
37    client_sme_proxy
38}
39
40pub async fn get_telemetry(
41    generic_sme_proxy: &fidl_sme::GenericSmeProxy,
42) -> fidl_sme::TelemetryProxy {
43    let (telemetry_proxy, telemetry_server) = fidl::endpoints::create_proxy();
44    generic_sme_proxy
45        .get_sme_telemetry(telemetry_server)
46        .await
47        .expect("FIDL error")
48        .expect("GetTelemetry error");
49    telemetry_proxy
50}
51
52pub async fn get_ap_sme(generic_sme_proxy: &fidl_sme::GenericSmeProxy) -> fidl_sme::ApSmeProxy {
53    let (ap_sme_proxy, ap_sme_server) = fidl::endpoints::create_proxy();
54    generic_sme_proxy.get_ap_sme(ap_sme_server).await.expect("FIDL error").expect("GetApSme Error");
55    ap_sme_proxy
56}
57
58pub fn random_string_as_bytes(len: usize) -> Vec<u8> {
59    rand::thread_rng().sample_iter(&Alphanumeric).take(len).collect()
60}
61
62pub fn random_ssid() -> Vec<u8> {
63    let ssid_len = rand::thread_rng().gen_range(1..=fidl_ieee80211::MAX_SSID_BYTE_LEN as usize);
64    random_string_as_bytes(ssid_len)
65}
66
67pub fn random_password() -> Vec<u8> {
68    let pw_len = rand::thread_rng().gen_range(8..=63);
69    random_string_as_bytes(pw_len)
70}