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    InternalRuntimeMutexLocked,
39    /// The given memory layout falls outside of the acceptable range
40    BadMemoryLayout,
41    ModemAlreadyInitialized,
42    /// The modem has a maximum packet size of 2kb when receiving TLS packets
43    TlsPacketTooBig,
44    /// tcp and udp TLS connections require at least one security tag to identify the server certificate
45    NoSecurityTag,
46    #[cfg(feature = "dns-async")]
47    DomainNameTooLong,
48    #[cfg(feature = "dns-async")]
49    DnsCacheOverflow,
50    #[cfg(feature = "dns-async")]
51    DnsHeaderBufferOverflow,
52    #[cfg(feature = "dns-async")]
53    DnsQuestionBufferOverflow,
54    #[cfg(feature = "dns-async")]
55    DnsSocketTimeout,
56    #[cfg(feature = "dns-async")]
57    DnsSocketError,
58    #[cfg(feature = "dns-async")]
59    DnsParseFailed,
60}
61
62impl embedded_io_async::Error for Error {
63    fn kind(&self) -> embedded_io_async::ErrorKind {
64        match self {
65            Error::ModemNotInitialized => embedded_io_async::ErrorKind::Other,
66            Error::GnssAlreadyTaken => embedded_io_async::ErrorKind::Other,
67            Error::NrfError(_) => embedded_io_async::ErrorKind::Other,
68            Error::BufferTooSmall(_) => embedded_io_async::ErrorKind::OutOfMemory,
69            Error::OutOfMemory => embedded_io_async::ErrorKind::OutOfMemory,
70            Error::AtParseError(_) => embedded_io_async::ErrorKind::Other,
71            Error::InvalidSystemModeConfig => embedded_io_async::ErrorKind::Other,
72            Error::StringNotNulTerminated => embedded_io_async::ErrorKind::InvalidInput,
73            Error::Utf8Error => embedded_io_async::ErrorKind::InvalidInput,
74            Error::LteRegistrationDenied => embedded_io_async::ErrorKind::Other,
75            Error::SimFailure => embedded_io_async::ErrorKind::Other,
76            Error::UnexpectedAtResponse => embedded_io_async::ErrorKind::Other,
77            Error::HostnameNotAscii => embedded_io_async::ErrorKind::InvalidInput,
78            Error::HostnameTooLong => embedded_io_async::ErrorKind::InvalidInput,
79            Error::AddressNotFound => embedded_io_async::ErrorKind::Other,
80            Error::SocketOptionError(_) => embedded_io_async::ErrorKind::Other,
81            Error::OperationCancelled => embedded_io_async::ErrorKind::Other,
82            Error::SmsNumberNotAscii => embedded_io_async::ErrorKind::Other,
83            Error::Disconnected => embedded_io_async::ErrorKind::ConnectionReset,
84            Error::TooManyLteLinks => embedded_io_async::ErrorKind::Other,
85            Error::InternalRuntimeMutexLocked => embedded_io_async::ErrorKind::Other,
86            Error::BadMemoryLayout => embedded_io_async::ErrorKind::Other,
87            Error::ModemAlreadyInitialized => embedded_io_async::ErrorKind::Other,
88            Error::TlsPacketTooBig => embedded_io_async::ErrorKind::Other,
89            Error::NoSecurityTag => embedded_io_async::ErrorKind::Other,
90            #[cfg(feature = "dns-async")]
91            Error::DomainNameTooLong => embedded_io_async::ErrorKind::InvalidInput,
92            #[cfg(feature = "dns-async")]
93            Error::DnsCacheOverflow => embedded_io_async::ErrorKind::Other,
94            #[cfg(feature = "dns-async")]
95            Error::DnsHeaderBufferOverflow => embedded_io_async::ErrorKind::Other,
96            #[cfg(feature = "dns-async")]
97            Error::DnsQuestionBufferOverflow => embedded_io_async::ErrorKind::Other,
98            #[cfg(feature = "dns-async")]
99            Error::DnsSocketTimeout => embedded_io_async::ErrorKind::TimedOut,
100            #[cfg(feature = "dns-async")]
101            Error::DnsSocketError => embedded_io_async::ErrorKind::Other,
102            #[cfg(feature = "dns-async")]
103            Error::DnsParseFailed => embedded_io_async::ErrorKind::Other,
104        }
105    }
106}
107
108pub trait ErrorSource {
109    fn into_result(self) -> Result<(), Error>;
110}
111
112impl ErrorSource for isize {
113    fn into_result(self) -> Result<(), Error> {
114        if self == 0 {
115            return Ok(());
116        }
117
118        Err(Error::NrfError(self))
119    }
120}
121impl ErrorSource for i32 {
122    fn into_result(self) -> Result<(), Error> {
123        if self == 0 {
124            return Ok(());
125        }
126
127        Err(Error::NrfError(self as isize))
128    }
129}
130
131impl From<ParseError> for Error {
132    fn from(e: ParseError) -> Self {
133        Error::AtParseError(e)
134    }
135}
136
137impl From<Utf8Error> for Error {
138    fn from(_: Utf8Error) -> Self {
139        Self::Utf8Error
140    }
141}
142
143impl From<SocketOptionError> for Error {
144    fn from(e: SocketOptionError) -> Self {
145        Self::SocketOptionError(e)
146    }
147}