-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
208 lines (167 loc) · 5.73 KB
/
Copy patherror.rs
File metadata and controls
208 lines (167 loc) · 5.73 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! Error types for the firmware.
use esp_idf_svc::sys::EspError;
use std::{fmt::Display, string::FromUtf8Error};
use thiserror::Error;
/// A wrapper for several different lower-level error types.
#[allow(clippy::doc_markdown)]
#[derive(Debug, Error)]
pub enum OsError {
/// Failed to initialize WiFi.
#[error("wifi init: {0}")]
WifiInit(EspError),
/// Failed to connect to WiFi AP.
#[error("wifi connect: {0}")]
WifiConnect(EspError),
/// Failed to set up a WiFi parameter.
#[error("wifi param: {0}")]
WifiParam(EspError),
/// Failed to set WiFi configuration.
#[error("wifi config: {0}")]
WifiConfig(EspError),
/// Failed to start the WiFi interface.
#[error("wifi start: {0}")]
WifiStart(EspError),
/// Failed to start AP scan or fetch the results.
#[error("wifi scan: {0}")]
WifiScan(EspError),
/// Failed to read WiFi interface information.
#[error("wifi info read: {0}")]
WifiInfo(EspError),
/// Failed to find password for the specified AP.
#[error("wifi psk not found")]
WifiPskNotFound,
/// Timeout while waiting for an event.
#[error("event timeout: {0}")]
EventTimeout(EspError),
/// Failed to initialize event waiter ([Wait](esp_idf_svc::eventloop::Wait)).
#[error("event waiter: {0}")]
EventWaiterInit(EspError),
/// No usable AP found.
#[error("offline")]
NoInternet,
/// Error during connecting to or processing data from the PWMP server.
#[error("pwmp: {0}")]
PwmpError(#[from] pwmp_client::error::Error),
/// No environment sensor detected.
#[error("environment sensor")]
NoEnvSensor,
/// OTA module or update initialization has failed.
#[error("Failed to initialize an OTA update ({0})")]
OtaInit(EspError),
/// Failed to write an OTA update chunk to flash.
#[error("OTA chunk write failed ({0})")]
OtaWrite(EspError),
/// Failed to abort OTA update.
#[error("OTA abort failed ({0})")]
OtaAbort(EspError),
/// An I/O operation on an OTA slot failed.
#[error("Failed to operate on an OTA slot ({0})")]
OtaSlot(EspError),
/// NVS initialization has failed
#[error("Failed to initialize NVS ({0})")]
NvsInit(EspError),
/// Error while reading from NVS.
#[error("Failed to read from NVS ({0})")]
NvsRead(EspError),
/// Error while writing to NVS.
#[error("Failed to write to NVS ({0})")]
NvsWrite(EspError),
/// Failed to initialize a GPIO pin.
#[error("Failed to initialize a GPIO pin ({0})")]
GpioInit(EspError),
/// Failed to initialize ADC.
#[error("Failed to initialize ADC ({0})")]
AdcInit(EspError),
/// Failed to read from the ADC.
#[error("Failed to read ADC ({0})")]
AdcRead(EspError),
/// Error while performing a write-read operation on I2C.
#[error("Failed to perform W/R on I2C ({esp_err}): Write {data:?} to addr {addr}")]
I2cWr {
data: heapless::Vec<u8, 4>,
addr: u8,
esp_err: EspError,
},
/// Error while performing a write operation on I2C.
#[error("Failed to perform write on I2C ({esp_err}): Write {data:?} to addr {addr}")]
I2cWrite {
data: heapless::Vec<u8, 4>,
addr: u8,
esp_err: EspError,
},
/// Specified parameter was too long.
#[error("Argument too long")]
ArgumentTooLong,
/// Partition metadata contains an invalid version string.
#[error("Unexpected version format")]
IllegalFirmwareVersion,
/// Partition metadata is missing.
#[error("Unexpected version format")]
MissingPartitionMetadata,
/// A buffer has been filled unexpectedly
#[error("A buffer capacity has been exceeded")]
UnexpectedBufferFailiure,
/// A value was `None`, when `Some(..)` was expected.
#[error("Unexpected NULL")]
UnexpectedNull,
/// Expected a UTF-8 string.
#[error("String is not UTF-8 encoded")]
InvalidUtf8(#[from] FromUtf8Error),
/// Key not found in NVS storage.
#[error("NVS key does not exist")]
InvalidNvsKey,
/// Failed to read data from internal temperature sensor.
#[error("Failed to read internal temperature sensor")]
InternalTempSensorRead(EspError),
}
/// Trait for non-fatal error types that can be "reported" to the console.
///
/// This trait is meant to be implemented for [`Result`]s.
pub trait ReportableError {
/// Log a warning to the console if the [`Result`] variant is an [`Err`], or do nothing if it's [`Ok`].
fn report(self, desc: &str);
}
impl<T, E: Display> ReportableError for Result<T, E> {
fn report(self, desc: &str) {
if let Err(why) = self {
log::warn!("{desc}: {why}");
}
}
}
impl OsError {
/// Returns whether the error is non-fatal.
///
/// This returns `true`, for errors related to WiFi/Internet and PWNP connectivity/IO issues.
pub const fn recoverable(&self) -> bool {
matches!(
self,
Self::WifiConnect(..) | Self::NoInternet | Self::PwmpError(..)
)
}
pub fn from_i2c_writeop(
result: Result<(), EspError>,
addr: u8,
data: &[u8],
wr: bool,
) -> Result<(), Self> {
let Err(why) = result else {
return Ok(());
};
let mut data_copy = heapless::Vec::<u8, 4>::new();
// SAFETY: The copy operation will copy only as much as the vec can hold.
unsafe { data_copy.extend_from_slice(data).unwrap_unchecked() };
if wr {
Err(Self::I2cWr {
data: data_copy,
addr,
esp_err: why,
})
} else {
Err(Self::I2cWrite {
data: data_copy,
addr,
esp_err: why,
})
}
}
}