Skip to content

Commit 36634a4

Browse files
authored
Add Protocols to ClientConfig and EapClientConfig and apply them (#4080)
1 parent 779f7a8 commit 36634a4

File tree

1 file changed

+72
-26
lines changed

1 file changed

+72
-26
lines changed

esp-radio/src/wifi/mod.rs

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ pub enum Protocol {
210210
P802D11BGNAX,
211211
}
212212

213+
impl Protocol {
214+
fn to_mask(self) -> u32 {
215+
match self {
216+
Protocol::P802D11B => WIFI_PROTOCOL_11B,
217+
Protocol::P802D11BG => WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G,
218+
Protocol::P802D11BGN => WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N,
219+
Protocol::P802D11BGNLR => {
220+
WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR
221+
}
222+
Protocol::P802D11LR => WIFI_PROTOCOL_LR,
223+
Protocol::P802D11BGNAX => {
224+
WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_11AX
225+
}
226+
}
227+
}
228+
}
229+
213230
/// Secondary Wi-Fi channels.
214231
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd)]
215232
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -385,24 +402,6 @@ impl core::fmt::Debug for AccessPointConfig {
385402
#[cfg(feature = "defmt")]
386403
impl defmt::Format for AccessPointConfig {
387404
fn format(&self, fmt: defmt::Formatter<'_>) {
388-
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Default)]
389-
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
390-
pub struct ProtocolSet(EnumSet<Protocol>);
391-
392-
#[cfg(feature = "defmt")]
393-
impl defmt::Format for ProtocolSet {
394-
fn format(&self, fmt: defmt::Formatter<'_>) {
395-
for (i, p) in self.0.into_iter().enumerate() {
396-
if i > 0 {
397-
defmt::write!(fmt, " ");
398-
}
399-
defmt::write!(fmt, "{}", p);
400-
}
401-
}
402-
}
403-
404-
let protocol_set = ProtocolSet(self.protocols);
405-
406405
defmt::write!(
407406
fmt,
408407
"AccessPointConfiguration {{\
@@ -419,15 +418,15 @@ impl defmt::Format for AccessPointConfig {
419418
self.ssid_hidden,
420419
self.channel,
421420
self.secondary_channel,
422-
protocol_set,
421+
self.protocols,
423422
self.auth_method,
424423
self.max_connections
425424
);
426425
}
427426
}
428427

429428
/// Client configuration for a Wi-Fi connection.
430-
#[derive(BuilderLite, Clone, Default, Eq, PartialEq)]
429+
#[derive(BuilderLite, Clone, Eq, PartialEq)]
431430
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
432431
pub struct ClientConfig {
433432
/// The SSID of the Wi-Fi network.
@@ -446,6 +445,9 @@ pub struct ClientConfig {
446445

447446
/// The Wi-Fi channel to connect to.
448447
channel: Option<u8>,
448+
449+
/// The set of protocols supported by the access point.
450+
protocols: EnumSet<Protocol>,
449451
}
450452

451453
impl ClientConfig {
@@ -462,6 +464,19 @@ impl ClientConfig {
462464
}
463465
}
464466

467+
impl Default for ClientConfig {
468+
fn default() -> Self {
469+
ClientConfig {
470+
ssid: String::new(),
471+
bssid: None,
472+
auth_method: AuthMethod::Wpa2Personal,
473+
password: String::new(),
474+
channel: None,
475+
protocols: (Protocol::P802D11B | Protocol::P802D11BG | Protocol::P802D11BGN),
476+
}
477+
}
478+
}
479+
465480
impl core::fmt::Debug for ClientConfig {
466481
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
467482
f.debug_struct("ClientConfig")
@@ -470,6 +485,7 @@ impl core::fmt::Debug for ClientConfig {
470485
.field("auth_method", &self.auth_method)
471486
.field("password", &"**REDACTED**")
472487
.field("channel", &self.channel)
488+
.field("protocols", &self.protocols)
473489
.finish()
474490
}
475491
}
@@ -485,11 +501,13 @@ impl defmt::Format for ClientConfig {
485501
auth_method: {:?}, \
486502
password: **REDACTED**, \
487503
channel: {:?}, \
504+
protocols: {}, \
488505
}}",
489506
self.ssid.as_str(),
490507
self.bssid,
491508
self.auth_method,
492-
self.channel
509+
self.channel,
510+
self.protocols
493511
)
494512
}
495513
}
@@ -610,6 +628,9 @@ pub struct EapClientConfig {
610628

611629
/// The specific Wi-Fi channel to use for the connection.
612630
pub channel: Option<u8>,
631+
632+
/// The set of protocols supported by the access point.
633+
protocols: EnumSet<Protocol>,
613634
}
614635

615636
#[cfg(feature = "wifi-eap")]
@@ -657,6 +678,7 @@ impl Debug for EapClientConfig {
657678
.field("ca_cert set", &self.ca_cert.is_some())
658679
.field("certificate_and_key set", &"**REDACTED**")
659680
.field("ttls_phase2_method", &self.ttls_phase2_method)
681+
.field("protocols", &self.protocols)
660682
.finish()
661683
}
662684
}
@@ -682,6 +704,7 @@ impl defmt::Format for EapClientConfig {
682704
ca_cert: {}, \
683705
certificate_and_key: **REDACTED**, \
684706
ttls_phase2_method: {:?}, \
707+
protocols: {}, \
685708
}}",
686709
self.ssid.as_str(),
687710
self.bssid,
@@ -694,6 +717,7 @@ impl defmt::Format for EapClientConfig {
694717
self.pac_file,
695718
self.ca_cert,
696719
self.ttls_phase2_method,
720+
self.protocols
697721
)
698722
}
699723
}
@@ -716,6 +740,7 @@ impl Default for EapClientConfig {
716740
ca_cert: None,
717741
certificate_and_key: None,
718742
ttls_phase2_method: None,
743+
protocols: (Protocol::P802D11B | Protocol::P802D11BG | Protocol::P802D11BGN),
719744
}
720745
}
721746
}
@@ -2800,6 +2825,15 @@ impl WifiController<'_> {
28002825
Ok(())
28012826
}
28022827

2828+
fn apply_protocols(
2829+
iface: wifi_interface_t,
2830+
protocols: &EnumSet<Protocol>,
2831+
) -> Result<(), WifiError> {
2832+
let mask = protocols.iter().fold(0, |acc, p| acc | p.to_mask());
2833+
debug!("Setting protocols with mask {:b}", mask);
2834+
esp_wifi_result!(unsafe { esp_wifi_set_protocol(iface, mask as u8) })
2835+
}
2836+
28032837
/// Configures modem power saving.
28042838
pub fn set_power_saving(&mut self, ps: PowerSaveMode) -> Result<(), WifiError> {
28052839
apply_power_saving(ps)
@@ -2918,14 +2952,26 @@ impl WifiController<'_> {
29182952
esp_wifi_result!(unsafe { esp_wifi_set_mode(mode) })?;
29192953

29202954
match conf {
2921-
Config::None => Ok::<(), WifiError>(()),
2922-
Config::Client(config) => apply_sta_config(config),
2923-
Config::AccessPoint(config) => apply_ap_config(config),
2955+
Config::None => Ok(()),
2956+
Config::Client(config) => {
2957+
apply_sta_config(config)?;
2958+
Self::apply_protocols(wifi_interface_t_WIFI_IF_STA, &config.protocols)
2959+
}
2960+
Config::AccessPoint(config) => {
2961+
apply_ap_config(config)?;
2962+
Self::apply_protocols(wifi_interface_t_WIFI_IF_AP, &config.protocols)
2963+
}
29242964
Config::ApSta(sta_config, ap_config) => {
2925-
apply_ap_config(ap_config).and_then(|()| apply_sta_config(sta_config))
2965+
apply_ap_config(ap_config)?;
2966+
Self::apply_protocols(wifi_interface_t_WIFI_IF_AP, &ap_config.protocols)?;
2967+
apply_sta_config(sta_config)?;
2968+
Self::apply_protocols(wifi_interface_t_WIFI_IF_STA, &sta_config.protocols)
29262969
}
29272970
#[cfg(feature = "wifi-eap")]
2928-
Config::EapClient(config) => apply_sta_eap_config(config),
2971+
Config::EapClient(config) => {
2972+
apply_sta_eap_config(config)?;
2973+
Self::apply_protocols(wifi_interface_t_WIFI_IF_STA, &config.protocols)
2974+
}
29292975
}
29302976
.inspect_err(|_| {
29312977
// we/the driver might have applied a partial configuration

0 commit comments

Comments
 (0)