Skip to content

Commit

Permalink
Issue 333 - let return from wifi_sta_config_t return Option, as it mi…
Browse files Browse the repository at this point in the history
…ght not be possible to convert, eg auth_method unknown
  • Loading branch information
Empire committed Jan 1, 2024
1 parent 61ef1e4 commit abe3dee
Showing 1 changed file with 53 additions and 36 deletions.
89 changes: 53 additions & 36 deletions src/wifi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,24 +194,30 @@ impl TryFrom<&ClientConfiguration> for Newtype<wifi_sta_config_t> {
}
}

impl From<Newtype<wifi_sta_config_t>> for ClientConfiguration {
impl From<Newtype<wifi_sta_config_t>> for Option<ClientConfiguration> {
fn from(conf: Newtype<wifi_sta_config_t>) -> Self {
let auth_method: Option<AuthMethod> = Newtype(conf.0.threshold.authmode).into();

Self {
ssid: from_cstr(&conf.0.ssid).into(),
bssid: if conf.0.bssid_set {
Some(conf.0.bssid)
} else {
None
},
auth_method: auth_method.unwrap(),
password: from_cstr(&conf.0.password).try_into().unwrap(),
channel: if conf.0.channel != 0 {
Some(conf.0.channel)
} else {
None
let auth_method_opt: Option<AuthMethod> = Newtype(conf.0.threshold.authmode).into();
match auth_method_opt {
Some(auth_method) => {
Some(
ClientConfiguration {
ssid: from_cstr(&conf.0.ssid).into(),
bssid: if conf.0.bssid_set {
Some(conf.0.bssid)
} else {
None
},
auth_method: auth_method,
password: from_cstr(&conf.0.password).try_into().unwrap(),
channel: if conf.0.channel != 0 {
Some(conf.0.channel)
} else {
None
},
}
)
},
None => None,
}
}
}
Expand Down Expand Up @@ -239,26 +245,33 @@ impl TryFrom<&AccessPointConfiguration> for Newtype<wifi_ap_config_t> {
}
}

impl From<Newtype<wifi_ap_config_t>> for AccessPointConfiguration {
impl From<Newtype<wifi_ap_config_t>> for Option<AccessPointConfiguration> {
fn from(conf: Newtype<wifi_ap_config_t>) -> Self {
Self {
ssid: if conf.0.ssid_len == 0 {
from_cstr(&conf.0.ssid).try_into().unwrap()
} else {
unsafe {
core::str::from_utf8_unchecked(&conf.0.ssid[0..conf.0.ssid_len as usize])
.try_into()
.unwrap()
}
match Option::<AuthMethod>::from(Newtype(conf.0.authmode)) {
Some(auth_method) => {
Some(
AccessPointConfiguration {
ssid: if conf.0.ssid_len == 0 {
from_cstr(&conf.0.ssid).try_into().unwrap()
} else {
unsafe {
core::str::from_utf8_unchecked(&conf.0.ssid[0..conf.0.ssid_len as usize])
.try_into()
.unwrap()
}
},
ssid_hidden: conf.0.ssid_hidden != 0,
channel: conf.0.channel,
secondary_channel: None,
auth_method: auth_method,
protocols: EnumSet::<Protocol>::empty(), // TODO
password: from_cstr(&conf.0.password).try_into().unwrap(),
max_connections: conf.0.max_connection as u16,
}
)
},
ssid_hidden: conf.0.ssid_hidden != 0,
channel: conf.0.channel,
secondary_channel: None,
auth_method: Option::<AuthMethod>::from(Newtype(conf.0.authmode)).unwrap(),
protocols: EnumSet::<Protocol>::empty(), // TODO
password: from_cstr(&conf.0.password).try_into().unwrap(),
max_connections: conf.0.max_connection as u16,
}
None => None,
}
}
}

Expand Down Expand Up @@ -1129,8 +1142,12 @@ impl<'d> WifiDriver<'d> {
let mut wifi_config: wifi_config_t = Default::default();
esp!(unsafe { esp_wifi_get_config(wifi_interface_t_WIFI_IF_STA, &mut wifi_config) })?;

let result: ClientConfiguration = unsafe { Newtype(wifi_config.sta).into() };

let station_config: Option<ClientConfiguration>;
unsafe {
station_config = Newtype(wifi_config.sta).into();
}
//unwrap should be ok here, as we only convert a value that we were able to set prior
let result: ClientConfiguration = station_config.unwrap();
debug!("Providing STA configuration: {:?}", &result);

Ok(result)
Expand Down

0 comments on commit abe3dee

Please sign in to comment.