Skip to content

Commit

Permalink
Allow using AP when ps-max-modem is enabled (esp-rs#283)
Browse files Browse the repository at this point in the history
* Set inactive time only for ps-max-modem

* Set inactive time regardless of modem sleep mode

---------

Co-authored-by: bjoernQ <[email protected]>
  • Loading branch information
bugadani and bjoernQ committed May 24, 2024
1 parent 0fc619a commit 5a66d78
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
2 changes: 2 additions & 0 deletions esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ struct Config {
listen_interval: u16,
#[default(6)]
beacon_timeout: u16,
#[default(300)]
ap_beacon_timeout: u16,
}

#[cfg_attr(esp32, link_section = ".dram2_uninit")]
Expand Down
65 changes: 35 additions & 30 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use enumset::EnumSetType;
use esp_wifi_sys::include::esp_interface_t_ESP_IF_WIFI_AP;
use esp_wifi_sys::include::esp_wifi_disconnect;
use esp_wifi_sys::include::esp_wifi_get_mode;
use esp_wifi_sys::include::esp_wifi_set_inactive_time;
use esp_wifi_sys::include::esp_wifi_set_protocol;
use esp_wifi_sys::include::wifi_ap_config_t;
use esp_wifi_sys::include::wifi_auth_mode_t_WIFI_AUTH_WAPI_PSK;
Expand Down Expand Up @@ -641,10 +640,19 @@ unsafe extern "C" fn esp_wifi_tx_done_cb(
pub fn wifi_start() -> Result<(), WifiError> {
unsafe {
esp_wifi_result!(esp_wifi_start())?;
esp_wifi_result!(esp_wifi_set_inactive_time(
wifi_interface_t_WIFI_IF_STA,
crate::CONFIG.beacon_timeout
))?;

let mode = get_wifi_mode()?;
if mode.is_ap() {
esp_wifi_result!(esp_wifi_sys::include::esp_wifi_set_inactive_time(
wifi_interface_t_WIFI_IF_AP,
crate::CONFIG.ap_beacon_timeout
))?;
} else {
esp_wifi_result!(esp_wifi_sys::include::esp_wifi_set_inactive_time(
wifi_interface_t_WIFI_IF_STA,
crate::CONFIG.beacon_timeout
))?;
};

cfg_if::cfg_if! {
if #[cfg(feature = "ps-min-modem")] {
Expand Down Expand Up @@ -759,6 +767,18 @@ pub fn new<'d>(
new_with_config(&inited, device, Default::default())
}

pub(crate) fn get_wifi_mode() -> Result<WifiMode, WifiError> {
let mut mode = wifi_mode_t_WIFI_MODE_NULL;
esp_wifi_result!(unsafe { esp_wifi_get_mode(&mut mode) })?;

#[allow(non_upper_case_globals)]
match mode {
wifi_mode_t_WIFI_MODE_STA => Ok(WifiMode::Sta),
wifi_mode_t_WIFI_MODE_AP => Ok(WifiMode::Ap),
_ => Err(WifiError::UnknownWifiMode),
}
}

/// A wifi device implementing smoltcp's Device trait.
pub struct WifiDevice<'d> {
_device: PeripheralRef<'d, crate::hal::radio::Wifi>,
Expand All @@ -770,15 +790,7 @@ impl<'d> WifiDevice<'d> {
}

pub(crate) fn get_wifi_mode(&self) -> Result<WifiMode, WifiError> {
let mut mode = wifi_mode_t_WIFI_MODE_NULL;
esp_wifi_result!(unsafe { esp_wifi_get_mode(&mut mode) })?;

#[allow(non_upper_case_globals)]
match mode {
wifi_mode_t_WIFI_MODE_STA => Ok(WifiMode::Sta),
wifi_mode_t_WIFI_MODE_AP => Ok(WifiMode::Ap),
_ => Err(WifiError::UnknownWifiMode),
}
get_wifi_mode()
}
}

Expand Down Expand Up @@ -1037,18 +1049,9 @@ fn esp_wifi_can_send() -> bool {
// Casting const to mut is instant UB, even though in reality `esp_wifi_internal_tx` copies the buffer into its own memory and
// does not modify
pub fn esp_wifi_send_data(data: &mut [u8]) {
let mut wifi_mode = 0u32;
unsafe {
esp_wifi_get_mode(&mut wifi_mode);
}

#[allow(non_upper_case_globals)]
let is_ap = matches!(
wifi_mode,
wifi_mode_t_WIFI_MODE_AP | wifi_mode_t_WIFI_MODE_APSTA
);
let mode = unwrap!(get_wifi_mode());

let interface = if is_ap {
let interface = if mode.is_ap() {
wifi_interface_t_WIFI_IF_AP
} else {
wifi_interface_t_WIFI_IF_STA
Expand Down Expand Up @@ -1273,15 +1276,17 @@ fn dump_packet_info(buffer: &[u8]) {

#[macro_export]
macro_rules! esp_wifi_result {
($value:expr) => {
if $value != crate::binary::include::ESP_OK as i32 {
($value:expr) => {{
let result = $value;
if result != crate::binary::include::ESP_OK as i32 {
warn!("{} returned an error: {}", stringify!($value), result);
Err(WifiError::InternalError(unwrap!(FromPrimitive::from_i32(
$value
result
))))
} else {
core::result::Result::<(), WifiError>::Ok(())
Ok::<(), WifiError>(())
}
};
}};
}

#[cfg(feature = "embassy-net")]
Expand Down

0 comments on commit 5a66d78

Please sign in to comment.