use crate::readwrite::{BufferOverflow, ReadExactError};
#[derive(Debug, PartialEq)]
pub enum CommError {
Failed,
BufferError,
ProtocolByteError(&'static str, usize, u8, u8),
ReadError,
WriteError,
BufferReadError,
UnexpectedAddressFamily, Utf8Error(core::str::Utf8Error),
CapacityError(arrayvec::CapacityError),
BootRomStart,
FirmwareStart,
HifSendFailed,
InvalidHifResponse(&'static str),
OperationRetriesExceeded,
ExceedsFlashPageSize,
}
impl From<core::convert::Infallible> for CommError {
fn from(_: core::convert::Infallible) -> Self {
unreachable!("Infallible error")
}
}
impl From<core::str::Utf8Error> for CommError {
fn from(v: core::str::Utf8Error) -> Self {
Self::Utf8Error(v)
}
}
impl From<arrayvec::CapacityError> for CommError {
fn from(v: arrayvec::CapacityError) -> Self {
Self::CapacityError(v)
}
}
impl From<BufferOverflow> for CommError {
fn from(_: BufferOverflow) -> Self {
CommError::BufferError
}
}
impl<T> From<ReadExactError<T>> for CommError {
fn from(_: ReadExactError<T>) -> Self {
CommError::BufferReadError
}
}
impl core::fmt::Display for CommError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let msg = match self {
Self::Failed => "Operation failed",
Self::BufferError => "Buffer error",
Self::ProtocolByteError(loc, byte, expected, actual) => {
return write!(
f,
"Protocol byte error at {}, byte {}: expected {:#x}, got {:#x}",
loc, byte, expected, actual
);
}
Self::ReadError => "Read error",
Self::WriteError => "Write error",
Self::BufferReadError => "Buffer read error",
Self::UnexpectedAddressFamily => "Unexpected address family",
Self::Utf8Error(err) => return write!(f, "UTF-8 error: {}", err),
Self::CapacityError(err) => return write!(f, "Capacity error: {}", err),
Self::BootRomStart => "WiFi module boot ROM start failed",
Self::FirmwareStart => "WiFi module firmware start failed",
Self::HifSendFailed => "HIF send failed",
Self::InvalidHifResponse(err_str) => {
return write!(f, "Invalid {} response received.", err_str)
}
Self::OperationRetriesExceeded => "Operation retry limit exceeded.",
Self::ExceedsFlashPageSize => "The provided length exceeds the flash page size.",
};
f.write_str(msg)
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for CommError {
fn format(&self, f: defmt::Formatter) {
match self {
Self::Failed => defmt::write!(f, "Operation failed"),
Self::BufferError => defmt::write!(f, "Buffer error"),
Self::ProtocolByteError(loc, byte, expected, actual) => {
defmt::write!(
f,
"Protocol byte error at {}, byte {}: expected {:#x}, got {:#x}",
loc,
byte,
expected,
actual
);
}
Self::ReadError => defmt::write!(f, "Read error"),
Self::WriteError => defmt::write!(f, "Write error"),
Self::BufferReadError => defmt::write!(f, "Buffer read error"),
Self::UnexpectedAddressFamily => defmt::write!(f, "Unexpected address family"),
Self::Utf8Error(err) => defmt::write!(
f,
"UTF-8 error: invalid sequence at position {}, error length: {:?}",
err.valid_up_to(),
err.error_len()
),
Self::CapacityError(_) => defmt::write!(f, "Capacity error: array full"),
Self::BootRomStart => defmt::write!(f, "WiFi module boot ROM start failed"),
Self::FirmwareStart => defmt::write!(f, "WiFi module firmware start failed"),
Self::HifSendFailed => defmt::write!(f, "HIF send failed"),
Self::InvalidHifResponse(err_str) => {
defmt::write!(f, "Invalid {} response received.", err_str)
}
Self::ExceedsFlashPageSize => {
defmt::write!(f, "The provided length exceeds the flash page size.")
}
Self::OperationRetriesExceeded => defmt::write!(f, "Operation retry limit exceeded."),
}
}
}