nrf_modem/
error.rs

1use core::str::Utf8Error;
2
3use at_commands::parser::ParseError;
4
5use crate::socket::SocketOptionError;
6
7#[derive(Debug, Clone)]
8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9#[non_exhaustive]
10/// The global error type of this crate
11pub enum Error {
12    /// An operation was tried for which the modem needs to be initialized, but the modem is not yet initialized
13    ModemNotInitialized,
14    /// There can only be one Gnss instance, yet a second was requested
15    GnssAlreadyTaken,
16    /// An unkown error occured. Check [nrf_errno.h](https://github.com/nrfconnect/sdk-nrfxlib/blob/main/nrf_modem/include/nrf_errno.h) to see what it means.
17    ///
18    /// Sometimes the sign is flipped, but ignore that and just look at the number.
19    NrfError(isize),
20    BufferTooSmall(Option<usize>),
21    OutOfMemory,
22    AtParseError(ParseError),
23    InvalidSystemModeConfig,
24    StringNotNulTerminated,
25    Utf8Error,
26    LteRegistrationDenied,
27    SimFailure,
28    UnexpectedAtResponse,
29    HostnameNotAscii,
30    HostnameTooLong,
31    AddressNotFound,
32    SocketOptionError(SocketOptionError),
33    /// The ongoing operation has been cancelled by the user
34    OperationCancelled,
35    SmsNumberNotAscii,
36    Disconnected,
37    TooManyLteLinks,
38    TooManyUiccLinks,
39    InternalRuntimeMutexLocked,
40    /// The given memory layout falls outside of the acceptable range
41    BadMemoryLayout,
42    ModemAlreadyInitialized,
43    /// The modem has a maximum packet size of 2kb when receiving TLS packets
44    TlsPacketTooBig,
45    /// tcp and udp TLS connections require at least one security tag to identify the server certificate
46    NoSecurityTag,
47    #[cfg(feature = "dns-async")]
48    DomainNameTooLong,
49    #[cfg(feature = "dns-async")]
50    DnsCacheOverflow,
51    #[cfg(feature = "dns-async")]
52    DnsHeaderBufferOverflow,
53    #[cfg(feature = "dns-async")]
54    DnsQuestionBufferOverflow,
55    #[cfg(feature = "dns-async")]
56    DnsSocketTimeout,
57    #[cfg(feature = "dns-async")]
58    DnsSocketError,
59    #[cfg(feature = "dns-async")]
60    DnsParseFailed,
61    #[cfg(feature = "embedded-nal-async")]
62    ReverseDnsLookupNotSupported,
63}
64
65impl embedded_io_async::Error for Error {
66    fn kind(&self) -> embedded_io_async::ErrorKind {
67        match self {
68            Error::ModemNotInitialized => embedded_io_async::ErrorKind::Other,
69            Error::GnssAlreadyTaken => embedded_io_async::ErrorKind::Other,
70            Error::NrfError(_) => embedded_io_async::ErrorKind::Other,
71            Error::BufferTooSmall(_) => embedded_io_async::ErrorKind::OutOfMemory,
72            Error::OutOfMemory => embedded_io_async::ErrorKind::OutOfMemory,
73            Error::AtParseError(_) => embedded_io_async::ErrorKind::Other,
74            Error::InvalidSystemModeConfig => embedded_io_async::ErrorKind::Other,
75            Error::StringNotNulTerminated => embedded_io_async::ErrorKind::InvalidInput,
76            Error::Utf8Error => embedded_io_async::ErrorKind::InvalidInput,
77            Error::LteRegistrationDenied => embedded_io_async::ErrorKind::Other,
78            Error::SimFailure => embedded_io_async::ErrorKind::Other,
79            Error::UnexpectedAtResponse => embedded_io_async::ErrorKind::Other,
80            Error::HostnameNotAscii => embedded_io_async::ErrorKind::InvalidInput,
81            Error::HostnameTooLong => embedded_io_async::ErrorKind::InvalidInput,
82            Error::AddressNotFound => embedded_io_async::ErrorKind::Other,
83            Error::SocketOptionError(_) => embedded_io_async::ErrorKind::Other,
84            Error::OperationCancelled => embedded_io_async::ErrorKind::Other,
85            Error::SmsNumberNotAscii => embedded_io_async::ErrorKind::Other,
86            Error::Disconnected => embedded_io_async::ErrorKind::ConnectionReset,
87            Error::TooManyLteLinks => embedded_io_async::ErrorKind::Other,
88            Error::TooManyUiccLinks => embedded_io_async::ErrorKind::Other,
89            Error::InternalRuntimeMutexLocked => embedded_io_async::ErrorKind::Other,
90            Error::BadMemoryLayout => embedded_io_async::ErrorKind::Other,
91            Error::ModemAlreadyInitialized => embedded_io_async::ErrorKind::Other,
92            Error::TlsPacketTooBig => embedded_io_async::ErrorKind::Other,
93            Error::NoSecurityTag => embedded_io_async::ErrorKind::Other,
94            #[cfg(feature = "dns-async")]
95            Error::DomainNameTooLong => embedded_io_async::ErrorKind::InvalidInput,
96            #[cfg(feature = "dns-async")]
97            Error::DnsCacheOverflow => embedded_io_async::ErrorKind::Other,
98            #[cfg(feature = "dns-async")]
99            Error::DnsHeaderBufferOverflow => embedded_io_async::ErrorKind::Other,
100            #[cfg(feature = "dns-async")]
101            Error::DnsQuestionBufferOverflow => embedded_io_async::ErrorKind::Other,
102            #[cfg(feature = "dns-async")]
103            Error::DnsSocketTimeout => embedded_io_async::ErrorKind::TimedOut,
104            #[cfg(feature = "dns-async")]
105            Error::DnsSocketError => embedded_io_async::ErrorKind::Other,
106            #[cfg(feature = "dns-async")]
107            Error::DnsParseFailed => embedded_io_async::ErrorKind::Other,
108            #[cfg(feature = "embedded-nal-async")]
109            Error::ReverseDnsLookupNotSupported => embedded_io_async::ErrorKind::Unsupported,
110        }
111    }
112}
113
114pub trait ErrorSource {
115    fn into_result(self) -> Result<(), Error>;
116}
117
118impl ErrorSource for isize {
119    fn into_result(self) -> Result<(), Error> {
120        if self == 0 {
121            return Ok(());
122        }
123
124        Err(Error::NrfError(self))
125    }
126}
127impl ErrorSource for i32 {
128    fn into_result(self) -> Result<(), Error> {
129        if self == 0 {
130            return Ok(());
131        }
132
133        Err(Error::NrfError(self as isize))
134    }
135}
136
137impl From<ParseError> for Error {
138    fn from(e: ParseError) -> Self {
139        Error::AtParseError(e)
140    }
141}
142
143impl From<Utf8Error> for Error {
144    fn from(_: Utf8Error) -> Self {
145        Self::Utf8Error
146    }
147}
148
149impl From<SocketOptionError> for Error {
150    fn from(e: SocketOptionError) -> Self {
151        Self::SocketOptionError(e)
152    }
153}