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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Codec error types.
/// Errors that can occur when decoding a Modbus PDU.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DecodeError {
/// PDU is shorter than the minimum for the function code.
Truncated {
/// Minimum expected length.
expected: usize,
/// Actual length received.
actual: usize,
},
/// PDU has extra bytes for a fixed-length function code.
LengthMismatch {
/// Exact expected length.
expected: usize,
/// Actual length received.
actual: usize,
},
/// PDU exceeds the Modbus maximum of 253 bytes.
PduTooLarge {
/// Actual PDU length.
length: usize,
/// Maximum allowed PDU length.
maximum: usize,
},
/// Function code byte is not recognized.
UnknownFunctionCode(u8),
/// Byte count field does not match actual remaining data length.
ByteCountMismatch {
/// Byte count declared in the PDU.
declared: usize,
/// Actual remaining data length.
actual: usize,
},
/// Byte count field is outside the allowed range for this function code.
ByteCountOutOfRange {
/// The invalid byte count.
count: usize,
/// The minimum allowed byte count.
minimum: usize,
/// The maximum allowed byte count.
maximum: usize,
},
/// Quantity value is outside the allowed range for this function code.
QuantityOutOfRange {
/// The invalid quantity value.
quantity: u16,
},
/// Coil value is neither 0xFF00 nor 0x0000.
InvalidCoilValue(u16),
/// File sub-request reference type is not 6.
InvalidReferenceType(u8),
/// Packed file-record data cannot be split into valid sub-record groups.
InvalidFileRecordLength {
/// Invalid packed file-record byte length.
length: usize,
},
/// File-record address fields are outside the Modbus file-record model.
FileRecordOutOfRange {
/// File number.
file_number: u16,
/// Starting record number.
record_number: u16,
/// Number of records.
record_length: u16,
},
/// MEI type byte is not recognized.
UnknownMeiType(u8),
/// Exception code byte is not recognized.
UnknownExceptionCode(u8),
/// Diagnostic sub-function code is not recognized.
UnknownDiagnosticSubFunction(u16),
/// Diagnostics data is not an even number of bytes.
InvalidDiagnosticDataLength {
/// Invalid diagnostics data length.
length: usize,
},
/// Device ID code byte is not recognized (FC 0x2B / MEI 0x0E).
InvalidDeviceIdCode(u8),
/// Device ID conformity level byte is not recognized (FC 0x2B / MEI 0x0E).
InvalidDeviceIdConformityLevel(u8),
/// Device ID More Follows byte is neither 0x00 nor 0xFF.
InvalidDeviceIdMoreFollows(u8),
/// Device ID Next Object ID must be zero when More Follows is 0x00.
InvalidDeviceIdNextObjectId(u8),
/// Individual Device ID access must return exactly one object.
InvalidDeviceIdObjectCount(u8),
}
impl DecodeError {
/// Check that a fixed-length PDU data slice has exactly `expected` bytes.
pub(crate) fn check_exact_len(data: &[u8], expected: usize) -> Result<(), Self> {
match data.len().cmp(&expected) {
core::cmp::Ordering::Less => Err(Self::Truncated {
expected,
actual: data.len(),
}),
core::cmp::Ordering::Equal => Ok(()),
core::cmp::Ordering::Greater => Err(Self::LengthMismatch {
expected,
actual: data.len(),
}),
}
}
}
impl core::fmt::Display for DecodeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Truncated { expected, actual } => {
write!(f, "PDU truncated: expected {expected} bytes, got {actual}")
}
Self::LengthMismatch { expected, actual } => {
write!(
f,
"PDU length mismatch: expected {expected} bytes, got {actual}"
)
}
Self::PduTooLarge { length, maximum } => {
write!(f, "PDU too large: {length} bytes (maximum {maximum})")
}
Self::UnknownFunctionCode(fc) => write!(f, "unknown function code: {fc:#04X}"),
Self::ByteCountMismatch { declared, actual } => {
write!(
f,
"byte count mismatch: declared {declared}, actual {actual}"
)
}
Self::ByteCountOutOfRange {
count,
minimum,
maximum,
} => write!(
f,
"byte count out of range: {count} (expected {minimum}..={maximum})"
),
Self::QuantityOutOfRange { quantity } => {
write!(f, "quantity out of range: {quantity}")
}
Self::InvalidCoilValue(v) => write!(f, "invalid coil value: {v:#06X}"),
Self::InvalidReferenceType(rt) => write!(f, "invalid reference type: {rt}"),
Self::InvalidFileRecordLength { length } => {
write!(f, "invalid file-record data length: {length}")
}
Self::FileRecordOutOfRange {
file_number,
record_number,
record_length,
} => write!(
f,
"file record out of range: file {file_number}, record {record_number}, length {record_length}"
),
Self::UnknownMeiType(mt) => write!(f, "unknown MEI type: {mt:#04X}"),
Self::UnknownExceptionCode(ec) => write!(f, "unknown exception code: {ec:#04X}"),
Self::UnknownDiagnosticSubFunction(sf) => {
write!(f, "unknown diagnostic sub-function: {sf:#06X}")
}
Self::InvalidDiagnosticDataLength { length } => {
write!(
f,
"invalid diagnostics data length: {length} (expected a multiple of 2)"
)
}
Self::InvalidDeviceIdCode(code) => write!(f, "invalid device ID code: {code:#04X}"),
Self::InvalidDeviceIdConformityLevel(level) => {
write!(f, "invalid device ID conformity level: {level:#04X}")
}
Self::InvalidDeviceIdMoreFollows(value) => {
write!(f, "invalid device ID More Follows value: {value:#04X}")
}
Self::InvalidDeviceIdNextObjectId(object_id) => write!(
f,
"invalid device ID Next Object ID with More Follows = 0x00: {object_id:#04X}"
),
Self::InvalidDeviceIdObjectCount(count) => {
write!(f, "invalid individual device ID object count: {count}")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for DecodeError {}
/// Errors that can occur when encoding a Modbus PDU.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EncodeError {
/// Provided buffer is too small.
BufferTooSmall {
/// Required buffer size.
required: usize,
/// Available buffer size.
available: usize,
},
/// Encoded PDU would exceed the Modbus maximum of 253 bytes.
PduTooLarge {
/// Encoded PDU length.
length: usize,
/// Maximum allowed PDU length.
maximum: usize,
},
/// Quantity exceeds protocol limits.
QuantityOutOfRange {
/// The invalid quantity value.
quantity: u16,
},
/// Declared byte count does not match the payload length.
ByteCountMismatch {
/// Declared byte count.
declared: usize,
/// Actual payload length.
actual: usize,
},
/// Byte count is outside the allowed range for this function code.
ByteCountOutOfRange {
/// The invalid byte count.
count: usize,
/// The minimum allowed byte count.
minimum: usize,
/// The maximum allowed byte count.
maximum: usize,
},
/// File sub-request reference type is not 6.
InvalidReferenceType(u8),
/// Packed file-record data cannot be split into valid sub-record groups.
InvalidFileRecordLength {
/// Invalid packed file-record byte length.
length: usize,
},
/// File-record address fields are outside the Modbus file-record model.
FileRecordOutOfRange {
/// File number.
file_number: u16,
/// Starting record number.
record_number: u16,
/// Number of records.
record_length: u16,
},
/// Diagnostics data is not an even number of bytes.
InvalidDiagnosticDataLength {
/// Invalid diagnostics data length.
length: usize,
},
}
impl core::fmt::Display for EncodeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::BufferTooSmall {
required,
available,
} => {
write!(
f,
"buffer too small: need {required} bytes, have {available}"
)
}
Self::PduTooLarge { length, maximum } => {
write!(f, "PDU too large: {length} bytes (maximum {maximum})")
}
Self::QuantityOutOfRange { quantity } => {
write!(f, "quantity out of range: {quantity}")
}
Self::ByteCountMismatch { declared, actual } => {
write!(
f,
"byte count mismatch: declared {declared}, actual {actual}"
)
}
Self::ByteCountOutOfRange {
count,
minimum,
maximum,
} => write!(
f,
"byte count out of range: {count} (expected {minimum}..={maximum})"
),
Self::InvalidReferenceType(rt) => write!(f, "invalid reference type: {rt}"),
Self::InvalidFileRecordLength { length } => {
write!(f, "invalid file-record data length: {length}")
}
Self::FileRecordOutOfRange {
file_number,
record_number,
record_length,
} => write!(
f,
"file record out of range: file {file_number}, record {record_number}, length {record_length}"
),
Self::InvalidDiagnosticDataLength { length } => {
write!(
f,
"invalid diagnostics data length: {length} (expected a multiple of 2)"
)
}
}
}
}
impl EncodeError {
pub(crate) fn check_pdu_len(length: usize) -> Result<(), Self> {
let maximum = rusty_modbus_types::MAX_PDU_SIZE;
if length > maximum {
Err(Self::PduTooLarge { length, maximum })
} else {
Ok(())
}
}
pub(crate) fn check_quantity(quantity: u16, max: u16) -> Result<(), Self> {
if quantity == 0 || quantity > max {
Err(Self::QuantityOutOfRange { quantity })
} else {
Ok(())
}
}
pub(crate) fn check_byte_count(declared: usize, actual: usize) -> Result<(), Self> {
if declared == actual {
Ok(())
} else {
Err(Self::ByteCountMismatch { declared, actual })
}
}
pub(crate) fn check_byte_count_range(
count: usize,
minimum: usize,
maximum: usize,
) -> Result<(), Self> {
if (minimum..=maximum).contains(&count) {
Ok(())
} else {
Err(Self::ByteCountOutOfRange {
count,
minimum,
maximum,
})
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for EncodeError {}