Skip to main content

esp_radio/wifi/sta/
mod.rs

1//! Wi-Fi station.
2
3use procmacros::BuilderLite;
4
5use super::{AuthenticationMethod, DisconnectReason, Protocols, Ssid};
6use crate::{WifiError, wifi::AuthenticationMethodConfig};
7
8unstable_module!(
9    #[cfg(feature = "wifi-eap")]
10    #[cfg_attr(docsrs, doc(cfg(feature = "wifi-eap")))]
11    pub mod eap;
12);
13
14/// Wi-Fi scan method.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16#[cfg_attr(feature = "defmt", derive(defmt::Format))]
17#[non_exhaustive]
18#[repr(u8)]
19#[instability::unstable]
20pub enum ScanMethod {
21    /// Fast scan.
22    Fast,
23    /// Scan all channels.
24    AllChannels,
25}
26
27/// Station configuration for a Wi-Fi connection.
28#[derive(BuilderLite, Clone, Eq, PartialEq, Hash, Debug)]
29#[cfg_attr(feature = "defmt", derive(defmt::Format))]
30pub struct StationConfig {
31    /// The SSID of the Wi-Fi network.
32    pub(crate) ssid: Ssid,
33    /// The BSSID (MAC address) of the station.
34    pub(crate) bssid: Option<[u8; 6]>,
35    /// The authentication method for the Wi-Fi connection.
36    pub(crate) authentication: AuthenticationMethodConfig,
37    /// The Wi-Fi channel to connect to.
38    pub(crate) channel: Option<u8>,
39    /// The set of protocols supported by the access point.
40    pub(crate) protocols: Protocols,
41    /// Interval for station to listen to beacon from access point.
42    ///
43    /// The unit of listen interval is one beacon interval.
44    /// For example, if beacon interval is 100 ms and listen interval is 3,
45    /// the interval for station to listen to beacon is 300 ms
46    #[builder_lite(unstable)]
47    pub(crate) listen_interval: u16,
48    /// Time to disconnect from access point if no data is received.
49    ///
50    /// Must be between 6 and 31.
51    #[builder_lite(unstable)]
52    pub(crate) beacon_timeout: u16,
53    /// Number of connection retries station will do before moving to next access point.
54    ///
55    /// `scan_method` should be set as [`ScanMethod::AllChannels`] to use this config.
56    ///
57    /// Note: Enabling this may cause connection time to increase in case the best access point
58    /// doesn't behave properly.
59    #[builder_lite(unstable)]
60    pub(crate) failure_retry_cnt: u8,
61    /// Scan method.
62    #[builder_lite(unstable)]
63    pub(crate) scan_method: ScanMethod,
64}
65
66impl StationConfig {
67    pub(crate) fn validate(&self) -> Result<(), WifiError> {
68        if !(6..=31).contains(&self.beacon_timeout) {
69            return Err(WifiError::InvalidArguments);
70        }
71
72        Ok(())
73    }
74}
75
76impl Default for StationConfig {
77    fn default() -> Self {
78        StationConfig {
79            ssid: Ssid::default(),
80            bssid: None,
81            authentication: AuthenticationMethodConfig::Wpa2Personal(
82                "".try_into().expect("Password length is valid"),
83            ),
84            channel: None,
85            protocols: Protocols::default(),
86            listen_interval: 3,
87            beacon_timeout: 6,
88            failure_retry_cnt: 1,
89            scan_method: ScanMethod::Fast,
90        }
91    }
92}
93
94/// Information about the access point the station connected to.
95#[derive(Debug, Clone, PartialEq, Eq, Hash)]
96#[cfg_attr(feature = "defmt", derive(defmt::Format))]
97#[non_exhaustive]
98pub struct ConnectedInfo {
99    /// The SSID of the access point.
100    pub ssid: Ssid,
101    /// The BSSID of the access point.
102    pub bssid: [u8; 6],
103    /// The channel of the access point.
104    pub channel: u8,
105    /// The authentication method used.
106    pub authmode: AuthenticationMethod,
107    /// The Association ID (AID) assigned by the access point.
108    pub aid: u16,
109}
110
111/// Information about the access point the station disconnected from.
112#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
113#[cfg_attr(feature = "defmt", derive(defmt::Format))]
114#[non_exhaustive]
115pub struct DisconnectedInfo {
116    /// The SSID of the access point.
117    pub ssid: Ssid,
118    /// The BSSID of the access point.
119    pub bssid: [u8; 6],
120    /// The disconnect reason.
121    pub reason: DisconnectReason,
122    /// The RSSI.
123    pub rssi: i8,
124}