1use core::fmt;
14
15#[derive(Debug, PartialEq, Eq, Clone, Copy)]
17pub enum MbusError {
18 ParseError,
20 BasicParseError,
22 Timeout,
24 ModbusException(u8),
26 IoError,
28 Unexpected,
30 ConnectionLost,
32 UnsupportedFunction(u8),
34 ReservedSubFunction(u16),
36 InvalidPduLength,
38 InvalidAduLength,
40 ConnectionFailed,
42 ConnectionClosed,
44 BufferTooSmall,
46 BufferLenMissmatch,
48 SendFailed,
50 InvalidAddress,
52 InvalidOffset,
54 TooManyRequests,
56 InvalidFunctionCode,
58 NoRetriesLeft,
60 TooManyFileReadSubRequests,
62 FileReadPduOverflow,
64 UnexpectedResponse,
66 InvalidTransport,
68 InvalidSlaveAddress,
70 ChecksumError,
72 InvalidConfiguration,
74 InvalidNumOfExpectedRsps,
79 InvalidDataLen,
81 InvalidQuantity,
83 InvalidValue,
85 InvalidAndMask,
87 InvalidOrMask,
89 InvalidByteCount,
91 InvalidDeviceIdentification,
93 InvalidDeviceIdCode,
95 InvalidMeiType,
97 InvalidBroadcastAddress,
100 BroadcastNotAllowed,
105}
106
107impl MbusError {
108 pub const fn broadcast_not_allowed() -> Self {
113 Self::BroadcastNotAllowed
114 }
115}
116
117impl fmt::Display for MbusError {
118 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
119 match self {
120 MbusError::ParseError => write!(
121 f,
122 "Parse error: An error occurred while parsing the Modbus ADU"
123 ),
124 MbusError::BasicParseError => write!(
125 f,
126 "Basic parse error: The received frame is fundamentally malformed"
127 ),
128 MbusError::Timeout => write!(
129 f,
130 "Timeout: The transaction timed out waiting for a response"
131 ),
132 MbusError::ModbusException(code) => write!(
133 f,
134 "Modbus exception: The server responded with exception code 0x{:02X}",
135 code
136 ),
137 MbusError::IoError => write!(
138 f,
139 "I/O error: An I/O error occurred during TCP communication"
140 ),
141 MbusError::Unexpected => write!(f, "Unexpected error: An unexpected error occurred"),
142 MbusError::ConnectionLost => write!(
143 f,
144 "Connection lost: The connection was lost during an active transaction"
145 ),
146 MbusError::UnsupportedFunction(code) => write!(
147 f,
148 "Unsupported function: Function code 0x{:02X} is not supported",
149 code
150 ),
151 MbusError::ReservedSubFunction(code) => write!(
152 f,
153 "Reserved sub-function: Sub-function code 0x{:04X} is not available",
154 code
155 ),
156 MbusError::InvalidPduLength => {
157 write!(f, "Invalid PDU length: The PDU length is invalid")
158 }
159 MbusError::InvalidAduLength => {
160 write!(f, "Invalid ADU length: The ADU length is invalid")
161 }
162 MbusError::ConnectionFailed => write!(f, "Connection failed"),
163 MbusError::ConnectionClosed => write!(f, "Connection closed"),
164 MbusError::BufferTooSmall => {
165 write!(f, "Buffer too small: The data was too large for the buffer")
166 }
167 MbusError::BufferLenMissmatch => {
168 write!(f, "Buffer length mismatch: Buffer length is not matching")
169 }
170 MbusError::SendFailed => write!(f, "Send failed: Failed to send data"),
171 MbusError::InvalidAddress => write!(f, "Invalid address"),
172 MbusError::TooManyRequests => {
173 write!(f, "Too many requests: Expected responses buffer is full")
174 }
175 MbusError::InvalidFunctionCode => write!(f, "Invalid function code"),
176 MbusError::NoRetriesLeft => write!(f, "No retries left for the transaction"),
177 MbusError::TooManyFileReadSubRequests => write!(
178 f,
179 "Too many sub-requests: Maximum of 35 sub-requests per PDU allowed"
180 ),
181 MbusError::FileReadPduOverflow => write!(
182 f,
183 "File read PDU overflow: Total length of file read sub-requests exceeds maximum allowed bytes per PDU"
184 ),
185 MbusError::UnexpectedResponse => write!(
186 f,
187 "Unexpected response: An unexpected response was received"
188 ),
189 MbusError::InvalidTransport => write!(
190 f,
191 "Invalid transport: The transport is invalid for the requested operation"
192 ),
193 MbusError::InvalidSlaveAddress => write!(
194 f,
195 "Invalid slave address: The provided slave address is invalid"
196 ),
197 MbusError::ChecksumError => write!(
198 f,
199 "Checksum error: The received frame has an invalid checksum"
200 ),
201 MbusError::InvalidConfiguration => write!(
202 f,
203 "Invalid configuration: The provided configuration is invalid"
204 ),
205 MbusError::InvalidNumOfExpectedRsps => write!(
206 f,
207 "Invalid number of expected responses: for serial transports the queue size N must be exactly 1"
208 ),
209 MbusError::InvalidDataLen => write!(
210 f,
211 "Invalid data length: The provided data length is invalid"
212 ),
213 MbusError::InvalidQuantity => {
214 write!(f, "Invalid quantity: The provided quantity is invalid")
215 }
216 MbusError::InvalidValue => write!(f, "Invalid value: The provided value is invalid"),
217 MbusError::InvalidAndMask => {
218 write!(f, "Invalid AND mask: The provided AND mask is invalid")
219 }
220 MbusError::InvalidOrMask => {
221 write!(f, "Invalid OR mask: The provided OR mask is invalid")
222 }
223 MbusError::InvalidByteCount => {
224 write!(f, "Invalid byte count: The provided byte count is invalid")
225 }
226 MbusError::InvalidDeviceIdentification => write!(
227 f,
228 "Invalid device identification: The provided device identification is invalid"
229 ),
230 MbusError::InvalidDeviceIdCode => write!(
231 f,
232 "Invalid device ID code: The provided device ID code is invalid"
233 ),
234 MbusError::InvalidMeiType => {
235 write!(f, "Invalid MEI type: The provided MEI type is invalid")
236 }
237 MbusError::InvalidBroadcastAddress => write!(
238 f,
239 "Invalid broadcast address: The provided broadcast address (0) is invalid. Must use UnitIdOrSlaveAddr::new_broadcast_address() instead."
240 ),
241 MbusError::BroadcastNotAllowed => {
242 write!(f, "Broadcast not allowed: Broadcast not allowed")
243 }
244 MbusError::InvalidOffset => write!(f, "Invalid offset: The provided offset is invalid"),
245 }
246 }
247}
248
249impl core::error::Error for MbusError {}