forked from diondokter/nrf-modem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
169 lines (156 loc) · 6.56 KB
/
error.rs
File metadata and controls
169 lines (156 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use core::{fmt::Display, str::Utf8Error};
use at_commands::parser::ParseError;
use crate::socket::SocketOptionError;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
/// The global error type of this crate
pub enum Error {
/// An operation was tried for which the modem needs to be initialized, but the modem is not yet initialized
ModemNotInitialized,
/// There can only be one Gnss instance, yet a second was requested
GnssAlreadyTaken,
/// 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.
///
/// Sometimes the sign is flipped, but ignore that and just look at the number.
NrfError(isize),
BufferTooSmall(Option<usize>),
OutOfMemory,
AtParseError(ParseError),
InvalidSystemModeConfig,
StringNotNulTerminated,
Utf8Error,
LteRegistrationDenied,
SimFailure,
UnexpectedAtResponse,
HostnameNotAscii,
HostnameTooLong,
AddressNotFound,
SocketOptionError(SocketOptionError),
/// The ongoing operation has been cancelled by the user
OperationCancelled,
SmsNumberNotAscii,
Disconnected,
TooManyLteLinks,
TooManyUiccLinks,
InternalRuntimeMutexLocked,
/// The given memory layout falls outside of the acceptable range
BadMemoryLayout,
ModemAlreadyInitialized,
/// The modem has a maximum packet size of 2kb when receiving TLS packets
TlsPacketTooBig,
/// tcp and udp TLS connections require at least one security tag to identify the server certificate
NoSecurityTag,
#[cfg(feature = "dns-async")]
DomainNameTooLong,
#[cfg(feature = "dns-async")]
DnsCacheOverflow,
#[cfg(feature = "dns-async")]
DnsHeaderBufferOverflow,
#[cfg(feature = "dns-async")]
DnsQuestionBufferOverflow,
#[cfg(feature = "dns-async")]
DnsSocketTimeout,
#[cfg(feature = "dns-async")]
DnsSocketError,
#[cfg(feature = "dns-async")]
DnsParseFailed,
#[cfg(feature = "embedded-nal-async")]
ReverseDnsLookupNotSupported,
/// The address resturned by the modem couldn't be parsed
#[cfg(feature = "embassy-net")]
AddrParseError,
}
impl Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{self:?}")
}
}
impl core::error::Error for Error {}
impl embedded_io_async::Error for Error {
fn kind(&self) -> embedded_io_async::ErrorKind {
match self {
Error::ModemNotInitialized => embedded_io_async::ErrorKind::Other,
Error::GnssAlreadyTaken => embedded_io_async::ErrorKind::Other,
Error::NrfError(_) => embedded_io_async::ErrorKind::Other,
Error::BufferTooSmall(_) => embedded_io_async::ErrorKind::OutOfMemory,
Error::OutOfMemory => embedded_io_async::ErrorKind::OutOfMemory,
Error::AtParseError(_) => embedded_io_async::ErrorKind::Other,
Error::InvalidSystemModeConfig => embedded_io_async::ErrorKind::Other,
Error::StringNotNulTerminated => embedded_io_async::ErrorKind::InvalidInput,
Error::Utf8Error => embedded_io_async::ErrorKind::InvalidInput,
Error::LteRegistrationDenied => embedded_io_async::ErrorKind::Other,
Error::SimFailure => embedded_io_async::ErrorKind::Other,
Error::UnexpectedAtResponse => embedded_io_async::ErrorKind::Other,
Error::HostnameNotAscii => embedded_io_async::ErrorKind::InvalidInput,
Error::HostnameTooLong => embedded_io_async::ErrorKind::InvalidInput,
Error::AddressNotFound => embedded_io_async::ErrorKind::Other,
Error::SocketOptionError(_) => embedded_io_async::ErrorKind::Other,
Error::OperationCancelled => embedded_io_async::ErrorKind::Other,
Error::SmsNumberNotAscii => embedded_io_async::ErrorKind::Other,
Error::Disconnected => embedded_io_async::ErrorKind::ConnectionReset,
Error::TooManyLteLinks => embedded_io_async::ErrorKind::Other,
Error::TooManyUiccLinks => embedded_io_async::ErrorKind::Other,
Error::InternalRuntimeMutexLocked => embedded_io_async::ErrorKind::Other,
Error::BadMemoryLayout => embedded_io_async::ErrorKind::Other,
Error::ModemAlreadyInitialized => embedded_io_async::ErrorKind::Other,
Error::TlsPacketTooBig => embedded_io_async::ErrorKind::Other,
Error::NoSecurityTag => embedded_io_async::ErrorKind::Other,
#[cfg(feature = "dns-async")]
Error::DomainNameTooLong => embedded_io_async::ErrorKind::InvalidInput,
#[cfg(feature = "dns-async")]
Error::DnsCacheOverflow => embedded_io_async::ErrorKind::Other,
#[cfg(feature = "dns-async")]
Error::DnsHeaderBufferOverflow => embedded_io_async::ErrorKind::Other,
#[cfg(feature = "dns-async")]
Error::DnsQuestionBufferOverflow => embedded_io_async::ErrorKind::Other,
#[cfg(feature = "dns-async")]
Error::DnsSocketTimeout => embedded_io_async::ErrorKind::TimedOut,
#[cfg(feature = "dns-async")]
Error::DnsSocketError => embedded_io_async::ErrorKind::Other,
#[cfg(feature = "dns-async")]
Error::DnsParseFailed => embedded_io_async::ErrorKind::Other,
#[cfg(feature = "embedded-nal-async")]
Error::ReverseDnsLookupNotSupported => embedded_io_async::ErrorKind::Unsupported,
#[cfg(feature = "embassy-net")]
Error::AddrParseError => embedded_io_async::ErrorKind::InvalidInput,
}
}
}
/// Helper to convert [`nrfxlib_sys`] numeric errors into idiomatic results.
pub trait ErrorSource {
/// Turns a value of 0 into an [`Ok(())`][Result::Ok], whereas any other value gets turned into
/// [`Error::NrfError`].
fn into_result(self) -> Result<(), Error>;
}
impl ErrorSource for isize {
fn into_result(self) -> Result<(), Error> {
if self == 0 {
return Ok(());
}
Err(Error::NrfError(self))
}
}
impl ErrorSource for i32 {
fn into_result(self) -> Result<(), Error> {
if self == 0 {
return Ok(());
}
Err(Error::NrfError(self as isize))
}
}
impl From<ParseError> for Error {
fn from(e: ParseError) -> Self {
Error::AtParseError(e)
}
}
impl From<Utf8Error> for Error {
fn from(_: Utf8Error) -> Self {
Self::Utf8Error
}
}
impl From<SocketOptionError> for Error {
fn from(e: SocketOptionError) -> Self {
Self::SocketOptionError(e)
}
}