Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions esp-radio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- `phy_calibration_data` and `set_phy_calibration_data` are now marked as `unstable`. (#4463)
- The `InitializationError::General` is replaced by `InitializationError::Internal` (#4467)
- The `WifiError::InternalError` is removed and `InternalWifiError` was folded into `WifiError`, some variants renamed for clarity (#4467)

### Fixed

Expand Down
11 changes: 5 additions & 6 deletions esp-radio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ pub fn init<'d>() -> Result<Controller<'d>, InitializationError> {
#[cfg(coex)]
match crate::wifi::coex_initialize() {
0 => {}
error => return Err(InitializationError::General(error)),
error => return Err(InitializationError::Internal),
}

Ok(Controller {
Expand All @@ -358,9 +358,8 @@ fn is_interrupts_disabled() -> bool {
/// Error which can be returned during [`init`].
#[non_exhaustive]
pub enum InitializationError {
/// A general error occurred.
/// The internal error code is reported.
General(i32),
/// An internal error occurred.
Internal,
/// An error from the Wi-Fi driver.
#[cfg(feature = "wifi")]
WifiError(WifiError),
Expand All @@ -381,10 +380,10 @@ impl core::error::Error for InitializationError {}
impl core::fmt::Display for InitializationError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
InitializationError::General(e) => write!(f, "A general error {e} occurred"),
InitializationError::Internal => write!(f, "An internal error occurred"),
#[cfg(feature = "wifi")]
InitializationError::WifiError(e) => {
write!(f, "Wi-Fi driver related error occured: {e}")
write!(f, "Wi-Fi driver related error occurred: {e}")
}
InitializationError::WrongClockConfig => {
write!(f, "The current CPU clock frequency is too low")
Expand Down
229 changes: 144 additions & 85 deletions esp-radio/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,13 +1306,6 @@ pub(crate) static DATA_QUEUE_RX_STA: NonReentrantMutex<VecDeque<PacketBuffer>> =
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum WifiError {
/// Wi-Fi module is not initialized or not initialized for `Wi-Fi`
/// operations.
NotInitialized,

/// Internal Wi-Fi error.
InternalError(InternalWifiError),

/// The device disconnected from the network or failed to connect to it.
Disconnected,

Expand All @@ -1324,18 +1317,157 @@ pub enum WifiError {

/// Passed arguments are invalid.
InvalidArguments,

/// Generic failure - not further specified
Failed,

/// Out of memory
OutOfMemory,

/// Wi-Fi driver was not installed by [esp_wifi_init](crate::sys::include::esp_wifi_init)
NotInitialized,

/// Wi-Fi driver was not started by [esp_wifi_start]
NotStarted,

/// Wi-Fi driver was not stopped by [esp_wifi_stop]
NotStopped,

/// Wi-Fi interface error
Interface,

/// Wi-Fi mode error
Mode,

/// Wi-Fi internal state error
State,

/// Wi-Fi internal control block of station or soft-AP error
ControlBlock,

/// Wi-Fi internal NVS module error
Nvs,

/// MAC address is invalid
InvalidMac,

/// SSID is invalid
InvalidSsid,

/// Password is invalid
InvalidPassword,

/// Timeout error
Timeout,

/// Wi-Fi is in sleep state (RF closed) and wakeup failed
WakeFailed,

/// The caller would block
WouldBlock,

/// Station still in disconnect status
NotConnected,

/// Failed to post the event to Wi-Fi task
PostFail,

/// Invalid Wi-Fi state when init/deinit is called
InvalidInitState,

/// Returned when Wi-Fi is stopping
StopState,

/// The Wi-Fi connection is not associated
NotAssociated,

/// The Wi-Fi TX is disallowed
TxDisallowed,

/// An unknown error was reported by the Wi-Fi driver.
// This is here just in case we encounter an unmapped error - there doesn't seem to be a
// definitive and exhausting list of errors we should expect and panicking because of an
// unmapped error.
Unknown(i32),
}

impl WifiError {
fn from_error_code(code: i32) -> Self {
if crate::sys::include::ESP_FAIL == code {
return WifiError::Failed;
}

match code as u32 {
crate::sys::include::ESP_ERR_NO_MEM => WifiError::OutOfMemory,
crate::sys::include::ESP_ERR_INVALID_ARG => WifiError::InvalidArguments,
crate::sys::include::ESP_ERR_WIFI_NOT_INIT => WifiError::NotInitialized,
crate::sys::include::ESP_ERR_WIFI_NOT_STARTED => WifiError::NotStarted,
crate::sys::include::ESP_ERR_WIFI_NOT_STOPPED => WifiError::NotStopped,
crate::sys::include::ESP_ERR_WIFI_IF => WifiError::Interface,
crate::sys::include::ESP_ERR_WIFI_MODE => WifiError::Mode,
crate::sys::include::ESP_ERR_WIFI_STATE => WifiError::State,
crate::sys::include::ESP_ERR_WIFI_CONN => WifiError::ControlBlock,
crate::sys::include::ESP_ERR_WIFI_NVS => WifiError::Nvs,
crate::sys::include::ESP_ERR_WIFI_MAC => WifiError::InvalidMac,
crate::sys::include::ESP_ERR_WIFI_SSID => WifiError::InvalidSsid,
crate::sys::include::ESP_ERR_WIFI_PASSWORD => WifiError::InvalidPassword,
crate::sys::include::ESP_ERR_WIFI_TIMEOUT => WifiError::Timeout,
crate::sys::include::ESP_ERR_WIFI_WAKE_FAIL => WifiError::WakeFailed,
crate::sys::include::ESP_ERR_WIFI_WOULD_BLOCK => WifiError::WouldBlock,
crate::sys::include::ESP_ERR_WIFI_NOT_CONNECT => WifiError::NotConnected,
crate::sys::include::ESP_ERR_WIFI_POST => WifiError::PostFail,
crate::sys::include::ESP_ERR_WIFI_INIT_STATE => WifiError::InvalidInitState,
crate::sys::include::ESP_ERR_WIFI_STOP_STATE => WifiError::StopState,
crate::sys::include::ESP_ERR_WIFI_NOT_ASSOC => WifiError::NotAssociated,
crate::sys::include::ESP_ERR_WIFI_TX_DISALLOW => WifiError::TxDisallowed,
_ => WifiError::Unknown(code),
}
}
}

/// Events generated by the Wi-Fi driver.
impl core::fmt::Display for WifiError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
WifiError::NotInitialized => write!(f, "Wi-Fi not initialized."),
WifiError::InternalError(err) => write!(f, "Internal Wi-Fi error: {err:?}."),
WifiError::Disconnected => write!(f, "Wi-Fi disconnected."),
WifiError::UnknownWifiMode => write!(f, "Unknown Wi-Fi mode."),
WifiError::Unsupported => write!(f, "Unsupported operation or mode."),
WifiError::InvalidArguments => write!(f, "Invalid arguments."),
WifiError::Failed => write!(f, "Generic unspecified failure."),
WifiError::NotInitialized => write!(
f,
"Wi-Fi module is not initialized or not initialized for `Wi-Fi` operations."
),
WifiError::OutOfMemory => write!(f, "Out of memory."),
WifiError::NotStarted => write!(f, "Wi-Fi driver was not started."),
WifiError::NotStopped => write!(f, " Wi-Fi driver was not stopped."),
WifiError::Interface => write!(f, "Wi-Fi interface error."),
WifiError::Mode => write!(f, "Wi-Fi mode error."),
WifiError::State => write!(f, "Wi-Fi internal state error."),
WifiError::ControlBlock => write!(
f,
"Wi-Fi internal control block of station or soft-AP error."
),
WifiError::Nvs => write!(f, "Wi-Fi internal NVS module error."),
WifiError::InvalidMac => write!(f, "MAC address is invalid."),
WifiError::InvalidSsid => write!(f, "SSID is invalid."),
WifiError::InvalidPassword => write!(f, "Password is invalid."),
WifiError::Timeout => write!(f, "Timeout error."),
WifiError::WakeFailed => {
write!(f, "Wi-Fi is in sleep state (RF closed) and wakeup failed.")
}
WifiError::WouldBlock => write!(f, " The caller would block."),
WifiError::NotConnected => write!(f, "Station still in disconnect status."),
WifiError::PostFail => write!(f, "Failed to post the event to Wi-Fi task."),
WifiError::InvalidInitState => {
write!(f, "Invalid Wi-Fi state when init/deinit is called.")
}
WifiError::StopState => write!(f, "Returned when Wi-Fi is stopping."),
WifiError::NotAssociated => write!(f, "The Wi-Fi connection is not associated."),
WifiError::TxDisallowed => write!(f, "The Wi-Fi TX is disallowed."),
WifiError::Unknown(_) => {
write!(f, "An unknown error was reported by the Wi-Fi driver.")
}
}
}
}
Expand Down Expand Up @@ -1450,79 +1582,6 @@ pub enum WifiEvent {
StaNeighborRep,
}

/// Error originating from the underlying drivers
#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
#[repr(i32)]
pub enum InternalWifiError {
/// Out of memory
NoMem = 0x101,

/// Invalid argument
InvalidArg = 0x102,

/// Wi-Fi driver was not installed by [esp_wifi_init](crate::sys::include::esp_wifi_init)
NotInit = 0x3001,

/// Wi-Fi driver was not started by [esp_wifi_start]
NotStarted = 0x3002,

/// Wi-Fi driver was not stopped by [esp_wifi_stop]
NotStopped = 0x3003,

/// Wi-Fi interface error
Interface = 0x3004,

/// Wi-Fi mode error
Mode = 0x3005,

/// Wi-Fi internal state error
State = 0x3006,

/// Wi-Fi internal control block of station or soft-AP error
Conn = 0x3007,

/// Wi-Fi internal NVS module error
Nvs = 0x3008,

/// MAC address is invalid
InvalidMac = 0x3009,

/// SSID is invalid
InvalidSsid = 0x300A,

/// Password is invalid
InvalidPassword = 0x300B,

/// Timeout error
Timeout = 0x300C,

/// Wi-Fi is in sleep state (RF closed) and wakeup failed
WakeFail = 0x300D,

/// The caller would block
WouldBlock = 0x300E,

/// Station still in disconnect status
NotConnected = 0x300F,

/// Failed to post the event to Wi-Fi task
PostFail = 0x3012,

/// Invalid Wi-Fi state when init/deinit is called
InvalidInitState = 0x3013,

/// Returned when Wi-Fi is stopping
StopState = 0x3014,

/// The Wi-Fi connection is not associated
NotAssociated = 0x3015,

/// The Wi-Fi TX is disallowed
TxDisallowed = 0x3016,
}

/// Get the AP MAC address of the device.
pub fn ap_mac() -> [u8; 6] {
let mut mac = [0u8; 6];
Expand Down Expand Up @@ -2463,7 +2522,7 @@ macro_rules! esp_wifi_result {
error,
result
);
Err(WifiError::InternalError(error))
Err(WifiError::from_error_code(error))
} else {
Ok::<(), WifiError>(())
}
Expand Down Expand Up @@ -3442,7 +3501,7 @@ impl WifiController<'_> {
};

if config.auth_method == AuthMethod::None && !config.password.is_empty() {
return Err(WifiError::InternalError(InternalWifiError::InvalidArg));
return Err(WifiError::InvalidArguments);
}

unsafe {
Expand Down Expand Up @@ -3488,7 +3547,7 @@ impl WifiController<'_> {
};

if config.auth_method == AuthMethod::None && !config.password.is_empty() {
return Err(WifiError::InternalError(InternalWifiError::InvalidArg));
return Err(WifiError::InvalidArguments);
}

unsafe {
Expand Down
Loading