mbus-core 0.6.0

Modbus core functionalities to provide modbus-rs project
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! # Modbus Public Function Codes and Sub-functions
//!
//! This module defines the standard function codes and sub-function codes used in the
//! Modbus Application Protocol. It provides enums for:
//!
//! - **[`FunctionCode`]**: The primary operation identifier (e.g., Read Coils, Write Register).
//! - **[`DiagnosticSubFunction`]**: Sub-codes for serial-line diagnostics (FC 0x08).
//! - **[`EncapsulatedInterfaceType`]**: MEI types for tunneling other protocols (FC 0x2B).
//!
//! All types implement `TryFrom` for safe conversion from raw bytes and include
//! documentation referencing the Modbus Application Protocol Specification V1.1b3.
//!
//! This module is `no_std` compatible and uses `repr` attributes to ensure
//! memory layout matches the protocol's byte-level requirements.

use crate::errors::{ExceptionCode, MbusError};

/// Modbus Public Function Codes.
///
/// These are the standardized function codes defined in
/// the Modbus Application Protocol Specification V1.1b3.
///
/// See:
/// - Section 5.1 Public Function Code Definition
/// - Section 6.x for individual function behaviors
///
/// Reference: :contentReference[oaicite:1]{index=1}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum FunctionCode {
    // ============================================================
    // Bit Access (Single-bit data)
    // ============================================================
    /// 0x00 — Undefined
    /// This value is not defined in the specification and can be used as a placeholder
    /// for uninitialized or unknown function codes.
    /// It is not a valid function code for actual Modbus transactions.
    #[default]
    Default = 0x00, // Placeholder for uninitialized or unknown function code

    #[cfg(feature = "coils")]
    /// 0x01 — Read Coils
    ///
    /// Reads the ON/OFF status of discrete output coils.
    /// Section 6.1
    ReadCoils = 0x01,

    #[cfg(feature = "discrete-inputs")]
    /// 0x02 — Read Discrete Inputs
    ///
    /// Reads the ON/OFF status of discrete input contacts.
    /// Section 6.2
    ReadDiscreteInputs = 0x02,

    #[cfg(feature = "coils")]
    /// 0x05 — Write Single Coil
    ///
    /// Forces a single coil to ON (0xFF00) or OFF (0x0000).
    /// Section 6.5
    WriteSingleCoil = 0x05,

    #[cfg(feature = "coils")]
    /// 0x0F — Write Multiple Coils
    ///
    /// Forces multiple coils to ON/OFF.
    /// Section 6.11
    WriteMultipleCoils = 0x0F,

    // ============================================================
    // 16-bit Register Access
    // ============================================================
    #[cfg(feature = "registers")]
    /// 0x03 — Read Holding Registers
    ///
    /// Reads one or more 16-bit holding registers.
    /// Section 6.3
    ReadHoldingRegisters = 0x03,

    #[cfg(feature = "registers")]
    /// 0x04 — Read Input Registers
    ///
    /// Reads one or more 16-bit input registers.
    /// Section 6.4
    ReadInputRegisters = 0x04,

    /// 0x06 — Write Single Register
    #[cfg(feature = "registers")]
    ///
    /// Writes a single 16-bit holding register.
    /// Section 6.6
    WriteSingleRegister = 0x06,

    #[cfg(feature = "registers")]
    /// 0x10 — Write Multiple Registers
    ///
    /// Writes multiple 16-bit holding registers.
    /// Section 6.12
    WriteMultipleRegisters = 0x10,

    #[cfg(feature = "registers")]
    /// 0x16 — Mask Write Register
    ///
    /// Performs a bitwise mask write on a single register.
    /// Section 6.16
    MaskWriteRegister = 0x16,

    #[cfg(feature = "registers")]
    /// 0x17 — Read/Write Multiple Registers
    ///
    /// Reads and writes multiple registers in a single transaction.
    /// Section 6.17
    ReadWriteMultipleRegisters = 0x17,

    #[cfg(feature = "fifo")]
    /// 0x18 — Read FIFO Queue
    ///
    /// Reads the contents of a FIFO queue.
    /// Section 6.18
    ReadFifoQueue = 0x18,

    // ============================================================
    // File Record Access
    // ============================================================
    #[cfg(feature = "file-record")]
    /// 0x14 — Read File Record
    ///
    /// Reads structured file records.
    /// Section 6.14
    ReadFileRecord = 0x14,

    /// 0x15 — Write File Record
    #[cfg(feature = "file-record")]
    ///
    /// Writes structured file records.
    /// Section 6.15
    WriteFileRecord = 0x15,

    // ============================================================
    // Diagnostics & Device Information
    // ============================================================
    #[cfg(feature = "diagnostics")]
    /// 0x07 — Read Exception Status (Serial Line Only)
    ///
    /// Returns 8-bit exception status.
    /// Section 6.7
    ReadExceptionStatus = 0x07,

    #[cfg(feature = "diagnostics")]
    /// 0x08 — Diagnostics (Serial Line Only)
    ///
    /// Provides diagnostic and loopback tests.
    /// Requires sub-function codes.
    /// Section 6.8
    Diagnostics = 0x08,

    #[cfg(feature = "diagnostics")]
    /// 0x0B — Get Communication Event Counter (Serial Line Only)
    ///
    /// Returns communication event counter.
    /// Section 6.9
    GetCommEventCounter = 0x0B,

    #[cfg(feature = "diagnostics")]
    /// 0x0C — Get Communication Event Log (Serial Line Only)
    ///
    /// Returns communication event log.
    /// Section 6.10
    GetCommEventLog = 0x0C,

    #[cfg(feature = "diagnostics")]
    /// 0x11 — Report Server ID (Serial Line Only)
    ///
    /// Returns server identification.
    /// Section 6.13
    ReportServerId = 0x11,

    #[cfg(feature = "diagnostics")]
    /// 0x2B — Encapsulated Interface Transport
    ///
    /// Used for:
    /// - CANopen General Reference (Sub-function 0x0D)
    /// - Read Device Identification (Sub-function 0x0E)
    ///
    /// Section 6.19, 6.20, 6.21
    EncapsulatedInterfaceTransport = 0x2B,

    // ============================================================
    // Exception Responses (0x80 bit set)
    // ============================================================
    /// 0x81 — Exception Response for Read Coils (0x01 | 0x80)
    #[cfg(feature = "coils")]
    ReadCoilsException = 0x81,

    /// 0x82 — Exception Response for Read Discrete Inputs (0x02 | 0x80)
    #[cfg(feature = "discrete-inputs")]
    ReadDiscreteInputsException = 0x82,

    /// 0x83 — Exception Response for Read Holding Registers (0x03 | 0x80)
    #[cfg(feature = "registers")]
    ReadHoldingRegistersException = 0x83,

    /// 0x84 — Exception Response for Read Input Registers (0x04 | 0x80)
    #[cfg(feature = "registers")]
    ReadInputRegistersException = 0x84,

    /// 0x85 — Exception Response for Write Single Coil (0x05 | 0x80)
    #[cfg(feature = "coils")]
    WriteSingleCoilException = 0x85,

    /// 0x86 — Exception Response for Write Single Register (0x06 | 0x80)
    #[cfg(feature = "registers")]
    WriteSingleRegisterException = 0x86,

    /// 0x87 — Exception Response for Read Exception Status (0x07 | 0x80)
    #[cfg(feature = "diagnostics")]
    ReadExceptionStatusException = 0x87,

    /// 0x88 — Exception Response for Diagnostics (0x08 | 0x80)
    #[cfg(feature = "diagnostics")]
    DiagnosticsException = 0x88,

    /// 0x8B — Exception Response for Get Communication Event Counter (0x0B | 0x80)
    #[cfg(feature = "diagnostics")]
    GetCommEventCounterException = 0x8B,

    /// 0x8C — Exception Response for Get Communication Event Log (0x0C | 0x80)
    #[cfg(feature = "diagnostics")]
    GetCommEventLogException = 0x8C,

    /// 0x8F — Exception Response for Write Multiple Coils (0x0F | 0x80)
    #[cfg(feature = "coils")]
    WriteMultipleCoilsException = 0x8F,

    /// 0x90 — Exception Response for Write Multiple Registers (0x10 | 0x80)
    #[cfg(feature = "registers")]
    WriteMultipleRegistersException = 0x90,

    /// 0x91 — Exception Response for Report Server ID (0x11 | 0x80)
    #[cfg(feature = "diagnostics")]
    ReportServerIdException = 0x91,

    /// 0x94 — Exception Response for Read File Record (0x14 | 0x80)
    #[cfg(feature = "file-record")]
    ReadFileRecordException = 0x94,

    /// 0x95 — Exception Response for Write File Record (0x15 | 0x80)
    #[cfg(feature = "file-record")]
    WriteFileRecordException = 0x95,

    /// 0x96 — Exception Response for Mask Write Register (0x16 | 0x80)
    #[cfg(feature = "registers")]
    MaskWriteRegisterException = 0x96,

    /// 0x97 — Exception Response for Read/Write Multiple Registers (0x17 | 0x80)
    #[cfg(feature = "registers")]
    ReadWriteMultipleRegistersException = 0x97,

    /// 0x98 — Exception Response for Read FIFO Queue (0x18 | 0x80)
    #[cfg(feature = "fifo")]
    ReadFifoQueueException = 0x98,

    /// 0xAB — Exception Response for Encapsulated Interface Transport (0x2B | 0x80)
    #[cfg(feature = "diagnostics")]
    EncapsulatedInterfaceTransportException = 0xAB,
}

impl TryFrom<u8> for FunctionCode {
    type Error = MbusError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use FunctionCode::*;

        match value {
            #[cfg(feature = "coils")]
            0x01 => Ok(ReadCoils),
            #[cfg(feature = "discrete-inputs")]
            0x02 => Ok(ReadDiscreteInputs),
            #[cfg(feature = "registers")]
            0x03 => Ok(ReadHoldingRegisters),
            #[cfg(feature = "registers")]
            0x04 => Ok(ReadInputRegisters),
            #[cfg(feature = "coils")]
            0x05 => Ok(WriteSingleCoil),
            #[cfg(feature = "registers")]
            0x06 => Ok(WriteSingleRegister),
            #[cfg(feature = "diagnostics")]
            0x07 => Ok(ReadExceptionStatus),
            #[cfg(feature = "diagnostics")]
            0x08 => Ok(Diagnostics),
            #[cfg(feature = "diagnostics")]
            0x0B => Ok(GetCommEventCounter),
            #[cfg(feature = "diagnostics")]
            0x0C => Ok(GetCommEventLog),
            #[cfg(feature = "coils")]
            0x0F => Ok(WriteMultipleCoils),
            #[cfg(feature = "registers")]
            0x10 => Ok(WriteMultipleRegisters),
            #[cfg(feature = "diagnostics")]
            0x11 => Ok(ReportServerId),
            #[cfg(feature = "file-record")]
            0x14 => Ok(ReadFileRecord),
            #[cfg(feature = "file-record")]
            0x15 => Ok(WriteFileRecord),
            #[cfg(feature = "registers")]
            0x16 => Ok(MaskWriteRegister),
            #[cfg(feature = "registers")]
            0x17 => Ok(ReadWriteMultipleRegisters),
            #[cfg(feature = "fifo")]
            0x18 => Ok(ReadFifoQueue),
            #[cfg(feature = "diagnostics")]
            0x2B => Ok(EncapsulatedInterfaceTransport),
            // Exception responses (0x80 bit set)
            #[cfg(feature = "coils")]
            0x81 => Ok(ReadCoilsException),
            #[cfg(feature = "discrete-inputs")]
            0x82 => Ok(ReadDiscreteInputsException),
            #[cfg(feature = "registers")]
            0x83 => Ok(ReadHoldingRegistersException),
            #[cfg(feature = "registers")]
            0x84 => Ok(ReadInputRegistersException),
            #[cfg(feature = "coils")]
            0x85 => Ok(WriteSingleCoilException),
            #[cfg(feature = "registers")]
            0x86 => Ok(WriteSingleRegisterException),
            #[cfg(feature = "diagnostics")]
            0x87 => Ok(ReadExceptionStatusException),
            #[cfg(feature = "diagnostics")]
            0x88 => Ok(DiagnosticsException),
            #[cfg(feature = "diagnostics")]
            0x8B => Ok(GetCommEventCounterException),
            #[cfg(feature = "diagnostics")]
            0x8C => Ok(GetCommEventLogException),
            #[cfg(feature = "coils")]
            0x8F => Ok(WriteMultipleCoilsException),
            #[cfg(feature = "registers")]
            0x90 => Ok(WriteMultipleRegistersException),
            #[cfg(feature = "diagnostics")]
            0x91 => Ok(ReportServerIdException),
            #[cfg(feature = "file-record")]
            0x94 => Ok(ReadFileRecordException),
            #[cfg(feature = "file-record")]
            0x95 => Ok(WriteFileRecordException),
            #[cfg(feature = "registers")]
            0x96 => Ok(MaskWriteRegisterException),
            #[cfg(feature = "registers")]
            0x97 => Ok(ReadWriteMultipleRegistersException),
            #[cfg(feature = "fifo")]
            0x98 => Ok(ReadFifoQueueException),
            #[cfg(feature = "diagnostics")]
            0xAB => Ok(EncapsulatedInterfaceTransportException),
            _ => Err(MbusError::UnsupportedFunction(value)),
        }
    }
}

impl FunctionCode {
    /// Maps an application error to the corresponding Modbus exception code.
    ///
    /// This method determines the appropriate exception code to return based on the
    /// error that occurred during request processing. For errors that don't map to
    /// a specific exception code, `ServerDeviceFailure` is used as a default.
    ///
    /// # Arguments
    /// * `error` - The error that occurred during processing
    ///
    /// # Returns
    /// The Modbus exception code to send in the response
    ///
    /// # Example
    /// ```ignore
    /// let fc = FunctionCode::ReadHoldingRegisters;
    /// let error = MbusError::InvalidAddress;
    /// let exc_code = fc.exception_code_for_error(&error);
    /// assert_eq!(exc_code, ExceptionCode::IllegalDataAddress);
    /// ```
    pub fn exception_code_for_error(&self, error: &MbusError) -> ExceptionCode {
        match error {
            // Protocol/address errors
            MbusError::InvalidAddress | MbusError::InvalidOffset => {
                ExceptionCode::IllegalDataAddress
            }
            // Data length and parsing errors — the data field itself is malformed
            MbusError::InvalidDataLen
            | MbusError::ParseError
            | MbusError::BasicParseError
            | MbusError::InvalidPduLength => ExceptionCode::IllegalDataAddress,
            // Quantity/value errors
            MbusError::InvalidQuantity
            | MbusError::InvalidValue
            | MbusError::InvalidByteCount
            | MbusError::InvalidAndMask
            | MbusError::InvalidOrMask
            | MbusError::InvalidDeviceIdCode => ExceptionCode::IllegalDataValue,
            // Function code errors — also includes illegal sub-function / MEI types
            MbusError::InvalidFunctionCode
            | MbusError::UnsupportedFunction(_)
            | MbusError::ReservedSubFunction(_)
            | MbusError::InvalidMeiType
            | MbusError::BroadcastNotAllowed
            | MbusError::InvalidBroadcastAddress => ExceptionCode::IllegalFunction,
            // Default: all other errors map to server device failure
            _ => ExceptionCode::ServerDeviceFailure,
        }
    }

    /// Returns the exception function code variant (with 0x80 bit set) for this function code.
    ///
    /// Exception responses use function codes with the high bit (0x80) set to indicate
    /// that an exception occurred. This method maps normal function codes to their
    /// exception equivalents.
    ///
    /// # Returns
    /// The exception function code variant, or `None` if this is not a valid function code
    /// that can have exceptions.
    ///
    /// # Example
    /// ```ignore
    /// let fc = FunctionCode::ReadHoldingRegisters;
    /// let exc_fc = fc.exception_response();
    /// assert_eq!(exc_fc, Some(FunctionCode::ReadHoldingRegistersException));
    /// ```
    pub fn exception_response(&self) -> Option<FunctionCode> {
        match self {
            #[cfg(feature = "coils")]
            FunctionCode::ReadCoils => Some(FunctionCode::ReadCoilsException),
            #[cfg(feature = "discrete-inputs")]
            FunctionCode::ReadDiscreteInputs => Some(FunctionCode::ReadDiscreteInputsException),
            #[cfg(feature = "registers")]
            FunctionCode::ReadHoldingRegisters => Some(FunctionCode::ReadHoldingRegistersException),
            #[cfg(feature = "registers")]
            FunctionCode::ReadInputRegisters => Some(FunctionCode::ReadInputRegistersException),
            #[cfg(feature = "coils")]
            FunctionCode::WriteSingleCoil => Some(FunctionCode::WriteSingleCoilException),
            #[cfg(feature = "registers")]
            FunctionCode::WriteSingleRegister => Some(FunctionCode::WriteSingleRegisterException),
            #[cfg(feature = "diagnostics")]
            FunctionCode::ReadExceptionStatus => Some(FunctionCode::ReadExceptionStatusException),
            #[cfg(feature = "diagnostics")]
            FunctionCode::Diagnostics => Some(FunctionCode::DiagnosticsException),
            #[cfg(feature = "diagnostics")]
            FunctionCode::GetCommEventCounter => Some(FunctionCode::GetCommEventCounterException),
            #[cfg(feature = "diagnostics")]
            FunctionCode::GetCommEventLog => Some(FunctionCode::GetCommEventLogException),
            #[cfg(feature = "coils")]
            FunctionCode::WriteMultipleCoils => Some(FunctionCode::WriteMultipleCoilsException),
            #[cfg(feature = "registers")]
            FunctionCode::WriteMultipleRegisters => {
                Some(FunctionCode::WriteMultipleRegistersException)
            }
            #[cfg(feature = "diagnostics")]
            FunctionCode::ReportServerId => Some(FunctionCode::ReportServerIdException),
            #[cfg(feature = "file-record")]
            FunctionCode::ReadFileRecord => Some(FunctionCode::ReadFileRecordException),
            #[cfg(feature = "file-record")]
            FunctionCode::WriteFileRecord => Some(FunctionCode::WriteFileRecordException),
            #[cfg(feature = "registers")]
            FunctionCode::MaskWriteRegister => Some(FunctionCode::MaskWriteRegisterException),
            #[cfg(feature = "registers")]
            FunctionCode::ReadWriteMultipleRegisters => {
                Some(FunctionCode::ReadWriteMultipleRegistersException)
            }
            #[cfg(feature = "fifo")]
            FunctionCode::ReadFifoQueue => Some(FunctionCode::ReadFifoQueueException),
            #[cfg(feature = "diagnostics")]
            FunctionCode::EncapsulatedInterfaceTransport => {
                Some(FunctionCode::EncapsulatedInterfaceTransportException)
            }
            // Already exception codes or default
            _ => None,
        }
    }
}

/// Sub-function codes for Function Code 0x08 (Diagnostics).
///
/// Serial line only.
/// See Modbus Application Protocol Specification V1.1b3, Section 6.8.
///
/// These values are 16-bit and encoded big-endian inside the PDU data field.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum DiagnosticSubFunction {
    /// 0x0000 — Return Query Data (Loopback test)
    ReturnQueryData = 0x0000,

    /// 0x0001 — Restart Communications Option
    RestartCommunicationsOption = 0x0001,

    /// 0x0002 — Return Diagnostic Register
    ReturnDiagnosticRegister = 0x0002,

    /// 0x0003 — Change ASCII Input Delimiter
    ChangeAsciiInputDelimiter = 0x0003,

    /// 0x0004 — Force Listen Only Mode
    ForceListenOnlyMode = 0x0004,

    /// 0x000A — Clear Counters and Diagnostic Register
    ClearCountersAndDiagnosticRegister = 0x000A,

    /// 0x000B — Return Bus Message Count
    ReturnBusMessageCount = 0x000B,

    /// 0x000C — Return Bus Communication Error Count
    ReturnBusCommunicationErrorCount = 0x000C,

    /// 0x000D — Return Bus Exception Error Count
    ReturnBusExceptionErrorCount = 0x000D,

    /// 0x000E — Return Server Message Count
    ReturnServerMessageCount = 0x000E,

    /// 0x000F — Return Server No Response Count
    ReturnServerNoResponseCount = 0x000F,

    /// 0x0010 — Return Server NAK Count
    ReturnServerNakCount = 0x0010,

    /// 0x0011 — Return Server Busy Count
    ReturnServerBusyCount = 0x0011,

    /// 0x0012 — Return Bus Character Overrun Count
    ReturnBusCharacterOverrunCount = 0x0012,

    /// 0x0014 — Clear Overrun Counter and Flag
    ClearOverrunCounterAndFlag = 0x0014,
}

impl DiagnosticSubFunction {
    /// Converts the `DiagnosticSubFunction` enum variant into its 2-byte big-endian representation.
    pub fn to_be_bytes(self) -> [u8; 2] {
        (self as u16).to_be_bytes()
    }
}

impl From<DiagnosticSubFunction> for u16 {
    fn from(sub_func: DiagnosticSubFunction) -> Self {
        sub_func as u16
    }
}

impl TryFrom<u16> for DiagnosticSubFunction {
    type Error = MbusError;

    fn try_from(value: u16) -> Result<Self, Self::Error> {
        use DiagnosticSubFunction::*;

        match value {
            0x0000 => Ok(ReturnQueryData),
            0x0001 => Ok(RestartCommunicationsOption),
            0x0002 => Ok(ReturnDiagnosticRegister),
            0x0003 => Ok(ChangeAsciiInputDelimiter),
            0x0004 => Ok(ForceListenOnlyMode),

            // 0x0005–0x0009 Reserved
            0x000A => Ok(ClearCountersAndDiagnosticRegister),
            0x000B => Ok(ReturnBusMessageCount),
            0x000C => Ok(ReturnBusCommunicationErrorCount),
            0x000D => Ok(ReturnBusExceptionErrorCount),
            0x000E => Ok(ReturnServerMessageCount),
            0x000F => Ok(ReturnServerNoResponseCount),
            0x0010 => Ok(ReturnServerNakCount),
            0x0011 => Ok(ReturnServerBusyCount),
            0x0012 => Ok(ReturnBusCharacterOverrunCount),

            // 0x0013 Reserved
            0x0014 => Ok(ClearOverrunCounterAndFlag),

            // Everything else reserved per spec
            _ => Err(MbusError::ReservedSubFunction(value)),
        }
    }
}

/// MEI (Modbus Encapsulated Interface) types
/// for Function Code 0x2B.
///
/// See Section 6.19–6.21 of the specification.
///
/// Encoded as 1 byte following the function code.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum EncapsulatedInterfaceType {
    /// Placeholder default value used before a concrete MEI type is parsed.
    /// This value should not appear in a valid decoded protocol frame.
    #[default]
    Err,
    /// 0x0D — CANopen General Reference
    CanopenGeneralReference = 0x0D,

    /// 0x0E — Read Device Identification
    ReadDeviceIdentification = 0x0E,
}

impl From<EncapsulatedInterfaceType> for u8 {
    fn from(val: EncapsulatedInterfaceType) -> Self {
        val as u8
    }
}

impl TryFrom<u8> for EncapsulatedInterfaceType {
    type Error = MbusError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0x0D => Ok(Self::CanopenGeneralReference),
            0x0E => Ok(Self::ReadDeviceIdentification),
            _ => Err(MbusError::ReservedSubFunction(value as u16)),
        }
    }
}