1use core::fmt;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20#[repr(u8)]
21pub enum ExceptionCode {
22 IllegalFunction = 0x01,
24 IllegalDataAddress = 0x02,
26 IllegalDataValue = 0x03,
28 ServerDeviceFailure = 0x04,
30}
31
32impl From<ExceptionCode> for u8 {
33 fn from(code: ExceptionCode) -> Self {
34 code as u8
35 }
36}
37
38#[derive(Debug, PartialEq, Eq, Clone, Copy)]
40pub enum MbusError {
41 ParseError,
43 BasicParseError,
45 Timeout,
47 ModbusException(u8),
49 IoError,
51 Unexpected,
53 ConnectionLost,
55 UnsupportedFunction(u8),
57 ReservedSubFunction(u16),
59 InvalidPduLength,
61 InvalidAduLength,
63 ConnectionFailed,
65 ConnectionClosed,
67 BufferTooSmall,
69 BufferLenMissmatch,
71 SendFailed,
73 InvalidAddress,
75 InvalidOffset,
77 TooManyRequests,
79 InvalidFunctionCode,
81 NoRetriesLeft,
83 TooManyFileReadSubRequests,
85 FileReadPduOverflow,
87 UnexpectedResponse,
89 InvalidTransport,
91 InvalidSlaveAddress,
93 ChecksumError,
95 InvalidConfiguration,
97 InvalidNumOfExpectedRsps,
102 InvalidDataLen,
104 InvalidQuantity,
106 InvalidValue,
108 InvalidAndMask,
110 InvalidOrMask,
112 InvalidByteCount,
114 InvalidDeviceIdentification,
116 InvalidDeviceIdCode,
118 InvalidMeiType,
120 InvalidBroadcastAddress,
123 BroadcastNotAllowed,
128}
129
130impl MbusError {
131 pub const fn broadcast_not_allowed() -> Self {
136 Self::BroadcastNotAllowed
137 }
138}
139
140impl fmt::Display for MbusError {
141 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
142 match self {
143 MbusError::ParseError => write!(
144 f,
145 "Parse error: An error occurred while parsing the Modbus ADU"
146 ),
147 MbusError::BasicParseError => write!(
148 f,
149 "Basic parse error: The received frame is fundamentally malformed"
150 ),
151 MbusError::Timeout => write!(
152 f,
153 "Timeout: The transaction timed out waiting for a response"
154 ),
155 MbusError::ModbusException(code) => write!(
156 f,
157 "Modbus exception: The server responded with exception code 0x{:02X}",
158 code
159 ),
160 MbusError::IoError => write!(
161 f,
162 "I/O error: An I/O error occurred during TCP communication"
163 ),
164 MbusError::Unexpected => write!(f, "Unexpected error: An unexpected error occurred"),
165 MbusError::ConnectionLost => write!(
166 f,
167 "Connection lost: The connection was lost during an active transaction"
168 ),
169 MbusError::UnsupportedFunction(code) => write!(
170 f,
171 "Unsupported function: Function code 0x{:02X} is not supported",
172 code
173 ),
174 MbusError::ReservedSubFunction(code) => write!(
175 f,
176 "Reserved sub-function: Sub-function code 0x{:04X} is not available",
177 code
178 ),
179 MbusError::InvalidPduLength => {
180 write!(f, "Invalid PDU length: The PDU length is invalid")
181 }
182 MbusError::InvalidAduLength => {
183 write!(f, "Invalid ADU length: The ADU length is invalid")
184 }
185 MbusError::ConnectionFailed => write!(f, "Connection failed"),
186 MbusError::ConnectionClosed => write!(f, "Connection closed"),
187 MbusError::BufferTooSmall => {
188 write!(f, "Buffer too small: The data was too large for the buffer")
189 }
190 MbusError::BufferLenMissmatch => {
191 write!(f, "Buffer length mismatch: Buffer length is not matching")
192 }
193 MbusError::SendFailed => write!(f, "Send failed: Failed to send data"),
194 MbusError::InvalidAddress => write!(f, "Invalid address"),
195 MbusError::TooManyRequests => {
196 write!(f, "Too many requests: Expected responses buffer is full")
197 }
198 MbusError::InvalidFunctionCode => write!(f, "Invalid function code"),
199 MbusError::NoRetriesLeft => write!(f, "No retries left for the transaction"),
200 MbusError::TooManyFileReadSubRequests => write!(
201 f,
202 "Too many sub-requests: Maximum of 35 sub-requests per PDU allowed"
203 ),
204 MbusError::FileReadPduOverflow => write!(
205 f,
206 "File read PDU overflow: Total length of file read sub-requests exceeds maximum allowed bytes per PDU"
207 ),
208 MbusError::UnexpectedResponse => write!(
209 f,
210 "Unexpected response: An unexpected response was received"
211 ),
212 MbusError::InvalidTransport => write!(
213 f,
214 "Invalid transport: The transport is invalid for the requested operation"
215 ),
216 MbusError::InvalidSlaveAddress => write!(
217 f,
218 "Invalid slave address: The provided slave address is invalid"
219 ),
220 MbusError::ChecksumError => write!(
221 f,
222 "Checksum error: The received frame has an invalid checksum"
223 ),
224 MbusError::InvalidConfiguration => write!(
225 f,
226 "Invalid configuration: The provided configuration is invalid"
227 ),
228 MbusError::InvalidNumOfExpectedRsps => write!(
229 f,
230 "Invalid number of expected responses: for serial transports the queue size N must be exactly 1"
231 ),
232 MbusError::InvalidDataLen => write!(
233 f,
234 "Invalid data length: The provided data length is invalid"
235 ),
236 MbusError::InvalidQuantity => {
237 write!(f, "Invalid quantity: The provided quantity is invalid")
238 }
239 MbusError::InvalidValue => write!(f, "Invalid value: The provided value is invalid"),
240 MbusError::InvalidAndMask => {
241 write!(f, "Invalid AND mask: The provided AND mask is invalid")
242 }
243 MbusError::InvalidOrMask => {
244 write!(f, "Invalid OR mask: The provided OR mask is invalid")
245 }
246 MbusError::InvalidByteCount => {
247 write!(f, "Invalid byte count: The provided byte count is invalid")
248 }
249 MbusError::InvalidDeviceIdentification => write!(
250 f,
251 "Invalid device identification: The provided device identification is invalid"
252 ),
253 MbusError::InvalidDeviceIdCode => write!(
254 f,
255 "Invalid device ID code: The provided device ID code is invalid"
256 ),
257 MbusError::InvalidMeiType => {
258 write!(f, "Invalid MEI type: The provided MEI type is invalid")
259 }
260 MbusError::InvalidBroadcastAddress => write!(
261 f,
262 "Invalid broadcast address: The provided broadcast address (0) is invalid. Must use UnitIdOrSlaveAddr::new_broadcast_address() instead."
263 ),
264 MbusError::BroadcastNotAllowed => {
265 write!(f, "Broadcast not allowed: Broadcast not allowed")
266 }
267 MbusError::InvalidOffset => write!(f, "Invalid offset: The provided offset is invalid"),
268 }
269 }
270}
271
272impl core::error::Error for MbusError {}