Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RSDK-10288] sta disconnect events log #452

Open
wants to merge 7 commits into
base: RSDK-8990-platform-modernization
Choose a base branch
from

Conversation

mattjperez
Copy link
Member

@mattjperez mattjperez commented Mar 27, 2025

I took this approach with the enum because:

  1. all of the error codes are constants at the top level of esp_idf_sys
  2. they are defined as c_uint which are typically u32 however they can be u16 depending on the platform
  3. because enum discriminants are typically i32s, I wasn't confident that setting the variants directly would be consistent between the bindings and the enum (enum WifiError { Roaming = esp_idf_sys::wifi_err_ROAMING ... }).
  4. The wifi_event_sta_disconnected_t's reason field is defined as a u8 in the bindings

Most of the time was spent hand-rolling the enum a few times as I realized the above.

still needs to be tested to see that the error messages are present and useful

@mattjperez mattjperez requested a review from npmenard March 27, 2025 22:41
@mattjperez mattjperez marked this pull request as ready for review March 27, 2025 22:44
@mattjperez mattjperez requested a review from a team as a code owner March 27, 2025 22:44
let disconn: *const esp_idf_svc::sys::wifi_event_sta_disconnected_t =
unsafe { core::mem::transmute(ev_data) };
let reason: super::wifi_error::WifiErrReason = (*disconn).reason.into();
log::error!("sta disconnected: {:?}", reason);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the intention of this ticket (which was admittedly light on details) was to actually inspect the internal structure of the StaConnected and StaDisconnected events up around line 200 and report interesting context around these events (probably disconnection is generally more interesting).

Copy link
Member Author

@mattjperez mattjperez Mar 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's probably a better place than where I found originally, yeah.
I shifted the logging to there if you'd like to take a look.

So does that mean we want to look deeper into StaConnected as well for this ticket? It seems we would be able to log the ssid but not much else, https://docs.esp-rs.org/esp-idf-svc/esp_idf_svc/wifi/struct.StaConnectedRef.html.

I guess it could make sense given we now support multiple wifi, but need to test to see what the output is currently so we're not adding 'duplicate' logs

@mattjperez mattjperez requested a review from acmorrow March 28, 2025 14:53
sl_stack.subscribe::<WifiEvent, _>(move |event: WifiEvent| match event {
WifiEvent::StaDisconnected(disconnected) => {
let reason: super::wifi_error::WifiErrReason = disconnected.reason().into();
log::info!("StaDisconnected event received: {:?}", reason);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't bother writing StaDisconnected event here - it isn't really a meaningful bit of information for a consumer of the logs.

Instead, let's focus the log on user facing info: let's log the SSID, the reason, and, the RSSI. Something like:

log::info!("received a WiFi disconnection event for SSID {} (RSSI {}) with reason {}"

}
})?;
WifiEvent::StaConnected(_) => {
log::info!("wifi connected event received");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar thoughts. I think it is valuable to at least log the SSID here:

log::info!("received a WiFi connection event for SSID {}"

sl_stack.subscribe::<WifiEvent, _>(move |event: WifiEvent| match event {
WifiEvent::StaDisconnected(disconnected) => {
let reason: super::wifi_error::WifiErrReason = disconnected.reason().into();
log::info!("StaDisconnected event received: {:?}", reason);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From your slack message, I can see that logging the reason with {:?} is going to look like GroupKeyUpdateTimeout(16). However, the 16 isn't really helpful there, maybe. It makes it look like the 16 is somehow parameterizing the GroupKeyUpdateTimeout, but it is really just the underlying code. Maybe Debug on WifiErrReason isnt' the right way to log it.

Copy link
Member Author

@mattjperez mattjperez Mar 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can still see use for exposing the code itself, at least in the Unrecognized case I added as the catch-all. I can directly impl Display so we can still throw in a Debug statement if it's ever needed.

let subscription =
sl_stack.subscribe::<WifiEvent, _>(move |event: WifiEvent| match event {
WifiEvent::StaDisconnected(disconnected) => {
let reason: super::wifi_error::WifiErrReason = disconnected.reason().into();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe import it, since it will also get used in the connected event below per my comment?

use esp_idf_svc::sys::*;
use core::ffi::c_uint;

impl From<u16> for WifiErrReason {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather get a compilation error if we try to build on a platform where C's unsigned int isn't 32 bits. That's a really rare platform.

Comment on lines +94 to +118
wifi_err_reason_t_WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY => {
Self::NoApFoundWithCompatibleSecurity(v)
}
wifi_err_reason_t_WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD => {
Self::NoApFoundInAuthModeThreshold(v)
}
wifi_err_reason_t_WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD => {
Self::NoApFoundInRssiThreshold(v)
}
_ => Self::Unrecognized(v),
}
}
}

#[derive(Debug)]
pub enum WifiErrReason {
Unspecified(c_uint),
AuthExpire(c_uint),
AuthLeave(c_uint),
AssocExpire(c_uint),
AssocTooMany(c_uint),
NotAuthed(c_uint),
NotAssoced(c_uint),
AssocLeave(c_uint),
AssocNotAuthed(c_uint),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to wonder if there isn't some macro crate out there that can emit this kind of boilerplate for us given a C enum.

Also surprised that esp-idf-{svc,sys} doesn't do this for us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants