Skip to content

Commit

Permalink
Return capability based on configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 26, 2023
1 parent 080fd6d commit 5c76bd6
Showing 1 changed file with 43 additions and 40 deletions.
83 changes: 43 additions & 40 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::EspWifiInitialization;
use critical_section::{CriticalSection, Mutex};

use embedded_svc::wifi::{
AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Protocol,
SecondaryChannel, Wifi,
AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration,
Protocol, SecondaryChannel, Wifi,
};

use enumset::EnumSet;
Expand Down Expand Up @@ -137,15 +137,15 @@ impl WifiMode {
}
}

impl TryFrom<&embedded_svc::wifi::Configuration> for WifiMode {
impl TryFrom<&Configuration> for WifiMode {
type Error = WifiError;

fn try_from(config: &embedded_svc::wifi::Configuration) -> Result<Self, Self::Error> {
fn try_from(config: &Configuration) -> Result<Self, Self::Error> {
let mode = match config {
embedded_svc::wifi::Configuration::None => return Err(WifiError::UnknownWifiMode),
embedded_svc::wifi::Configuration::AccessPoint(_) => Self::Ap,
embedded_svc::wifi::Configuration::Client(_) => Self::Sta,
embedded_svc::wifi::Configuration::Mixed(_, _) => Self::ApSta,
Configuration::None => return Err(WifiError::UnknownWifiMode),
Configuration::AccessPoint(_) => Self::Ap,
Configuration::Client(_) => Self::Sta,
Configuration::Mixed(_, _) => Self::ApSta,
};

Ok(mode)
Expand Down Expand Up @@ -825,19 +825,18 @@ pub fn wifi_start_scan(block: bool) -> i32 {
/// Creates a new Wifi device and controller in either AP or STA mode with the given configuration.
///
/// This function will panic if the configuration is not
/// [`embedded_svc::wifi::Configuration::Client`] or [`embedded_svc::wifi::Configuration::Station`].
/// [`Configuration::Client`] or [`Configuration::Station`].
///
/// If you want to use AP-STA mode, use `[new_ap_sta]`.
pub fn new_with_config<'d>(
inited: &EspWifiInitialization,
device: impl Peripheral<P = crate::hal::radio::Wifi> + 'd,
config: embedded_svc::wifi::Configuration,
config: Configuration,
) -> Result<(WifiDevice<'d>, WifiController<'d>), WifiError> {
crate::hal::into_ref!(device);

match config {
embedded_svc::wifi::Configuration::None
| embedded_svc::wifi::Configuration::Mixed(_, _) => {
Configuration::None | Configuration::Mixed(_, _) => {
panic!("This constructor can not initialize the AP-STA mode")
}
_ => {}
Expand Down Expand Up @@ -865,8 +864,8 @@ pub fn new_with_mode<'d>(
inited,
device,
match mode {
WifiMode::Sta => embedded_svc::wifi::Configuration::Client(Default::default()),
WifiMode::Ap => embedded_svc::wifi::Configuration::AccessPoint(Default::default()),
WifiMode::Sta => Configuration::Client(Default::default()),
WifiMode::Ap => Configuration::AccessPoint(Default::default()),
WifiMode::ApSta => panic!("This constructor can not initialize the AP-STA mode"),
},
)
Expand All @@ -889,7 +888,7 @@ pub fn new_ap_sta<'d>(
WifiController::new_with_config(
inited,
device,
embedded_svc::wifi::Configuration::Mixed(sta_config, ap_config),
Configuration::Mixed(sta_config, ap_config),
)?,
))
}
Expand Down Expand Up @@ -1006,14 +1005,14 @@ fn convert_ap_info(record: &include::wifi_ap_record_t) -> AccessPointInfo {
/// A wifi controller implementing embedded_svc::Wifi traits
pub struct WifiController<'d> {
_device: PeripheralRef<'d, crate::hal::radio::Wifi>,
config: embedded_svc::wifi::Configuration,
config: Configuration,
}

impl<'d> WifiController<'d> {
pub(crate) fn new_with_config(
inited: &EspWifiInitialization,
_device: PeripheralRef<'d, crate::hal::radio::Wifi>,
config: embedded_svc::wifi::Configuration,
config: Configuration,
) -> Result<Self, WifiError> {
if !inited.is_wifi() {
return Err(WifiError::NotInitialized);
Expand Down Expand Up @@ -1313,10 +1312,17 @@ impl Wifi for WifiController<'_> {

/// This currently only supports the `Client` and `AccessPoint` capability.
fn get_capabilities(&self) -> Result<EnumSet<embedded_svc::wifi::Capability>, Self::Error> {
// we only support STA and AP mode
let mut caps = EnumSet::empty();
caps.insert(embedded_svc::wifi::Capability::Client);
caps.insert(embedded_svc::wifi::Capability::AccessPoint);
use embedded_svc::wifi::Capability;

let caps = match self.config {
Configuration::None => unreachable!(),
Configuration::Client(_) => enumset::enum_set! { Capability::Client },
Configuration::AccessPoint(_) => (enumset::enum_set! { Capability::AccessPoint }),
Configuration::Mixed(_, _) => {
Capability::Client | Capability::AccessPoint | Capability::Mixed
}
};

Ok(caps)
}

Expand All @@ -1333,57 +1339,54 @@ impl Wifi for WifiController<'_> {
}

/// Get the currently used configuration.
fn get_configuration(&self) -> Result<embedded_svc::wifi::Configuration, Self::Error> {
fn get_configuration(&self) -> Result<Configuration, Self::Error> {
Ok(self.config.clone())
}

/// Set the configuration, you need to use Wifi::connect() for connecting to an AP
/// Trying anything but `Configuration::Client` or `Configuration::AccessPoint` will result in a panic!
fn set_configuration(
&mut self,
conf: &embedded_svc::wifi::Configuration,
) -> Result<(), Self::Error> {
fn set_configuration(&mut self, conf: &Configuration) -> Result<(), Self::Error> {
match self.config {
embedded_svc::wifi::Configuration::None => self.config = conf.clone(), // initial config
embedded_svc::wifi::Configuration::Client(ref mut client) => {
if let embedded_svc::wifi::Configuration::Client(conf) = conf {
Configuration::None => self.config = conf.clone(), // initial config
Configuration::Client(ref mut client) => {
if let Configuration::Client(conf) = conf {
*client = conf.clone();
} else {
return Err(WifiError::InternalError(
InternalWifiError::EspErrInvalidArg,
));
}
}
embedded_svc::wifi::Configuration::AccessPoint(ref mut ap) => {
if let embedded_svc::wifi::Configuration::AccessPoint(conf) = conf {
Configuration::AccessPoint(ref mut ap) => {
if let Configuration::AccessPoint(conf) = conf {
*ap = conf.clone();
} else {
return Err(WifiError::InternalError(
InternalWifiError::EspErrInvalidArg,
));
}
}
embedded_svc::wifi::Configuration::Mixed(ref mut client, ref mut ap) => match conf {
embedded_svc::wifi::Configuration::None => {
Configuration::Mixed(ref mut client, ref mut ap) => match conf {
Configuration::None => {
return Err(WifiError::InternalError(
InternalWifiError::EspErrInvalidArg,
));
}
embedded_svc::wifi::Configuration::Mixed(_, _) => self.config = conf.clone(),
embedded_svc::wifi::Configuration::Client(conf) => *client = conf.clone(),
embedded_svc::wifi::Configuration::AccessPoint(conf) => *ap = conf.clone(),
Configuration::Mixed(_, _) => self.config = conf.clone(),
Configuration::Client(conf) => *client = conf.clone(),
Configuration::AccessPoint(conf) => *ap = conf.clone(),
},
}

match conf {
embedded_svc::wifi::Configuration::None => {
Configuration::None => {
return Err(WifiError::InternalError(
InternalWifiError::EspErrInvalidArg,
));
}
embedded_svc::wifi::Configuration::Client(config) => apply_sta_config(config)?,
embedded_svc::wifi::Configuration::AccessPoint(config) => apply_ap_config(config)?,
embedded_svc::wifi::Configuration::Mixed(sta_config, ap_config) => {
Configuration::Client(config) => apply_sta_config(config)?,
Configuration::AccessPoint(config) => apply_ap_config(config)?,
Configuration::Mixed(sta_config, ap_config) => {
apply_ap_config(ap_config)?;
apply_sta_config(sta_config)?;
}
Expand Down

0 comments on commit 5c76bd6

Please sign in to comment.