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
use core::fmt;

mod coils;
mod data;
pub(crate) mod rtu;
pub(crate) mod tcp;

pub use self::{coils::*, data::*};
use byteorder::{BigEndian, ByteOrder};

/// A Modbus function code.
///
/// It is represented by an unsigned 8 bit integer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FunctionCode {
    /// Modbus Function Code: `01` (`0x01`).
    ReadCoils,

    /// Modbus Function Code: `02` (`0x02`).
    ReadDiscreteInputs,

    /// Modbus Function Code: `05` (`0x05`).
    WriteSingleCoil,

    /// Modbus Function Code: `06` (`0x06`).
    WriteSingleRegister,

    /// Modbus Function Code: `03` (`0x03`).
    ReadHoldingRegisters,

    /// Modbus Function Code: `04` (`0x04`).
    ReadInputRegisters,

    /// Modbus Function Code: `15` (`0x0F`).
    WriteMultipleCoils,

    /// Modbus Function Code: `16` (`0x10`).
    WriteMultipleRegisters,

    /// Modbus Function Code: `22` (`0x16`).
    MaskWriteRegister,

    /// Modbus Function Code: `23` (`0x17`).
    ReadWriteMultipleRegisters,

    #[cfg(feature = "rtu")]
    ReadExceptionStatus,

    #[cfg(feature = "rtu")]
    Diagnostics,

    #[cfg(feature = "rtu")]
    GetCommEventCounter,

    #[cfg(feature = "rtu")]
    GetCommEventLog,

    #[cfg(feature = "rtu")]
    ReportServerId,

    // TODO:
    // - ReadFileRecord
    // - WriteFileRecord
    // TODO:
    // - Read FifoQueue
    // - EncapsulatedInterfaceTransport
    // - CanOpenGeneralReferenceRequestAndResponsePdu
    // - ReadDeviceIdentification
    /// Custom Modbus Function Code.
    Custom(u8),
}

impl FunctionCode {
    /// Create a new [`FunctionCode`] with `value`.
    #[must_use]
    pub const fn new(value: u8) -> Self {
        match value {
            0x01 => Self::ReadCoils,
            0x02 => Self::ReadDiscreteInputs,
            0x05 => Self::WriteSingleCoil,
            0x06 => Self::WriteSingleRegister,
            0x03 => Self::ReadHoldingRegisters,
            0x04 => Self::ReadInputRegisters,
            0x0F => Self::WriteMultipleCoils,
            0x10 => Self::WriteMultipleRegisters,
            0x16 => Self::MaskWriteRegister,
            0x17 => Self::ReadWriteMultipleRegisters,
            #[cfg(feature = "rtu")]
            0x07 => Self::ReadExceptionStatus,
            #[cfg(feature = "rtu")]
            0x08 => Self::Diagnostics,
            #[cfg(feature = "rtu")]
            0x0B => Self::GetCommEventCounter,
            #[cfg(feature = "rtu")]
            0x0C => Self::GetCommEventLog,
            #[cfg(feature = "rtu")]
            0x11 => Self::ReportServerId,
            code => FunctionCode::Custom(code),
        }
    }

    /// Get the [`u8`] value of the current [`FunctionCode`].
    #[must_use]
    pub const fn value(self) -> u8 {
        match self {
            Self::ReadCoils => 0x01,
            Self::ReadDiscreteInputs => 0x02,
            Self::WriteSingleCoil => 0x05,
            Self::WriteSingleRegister => 0x06,
            Self::ReadHoldingRegisters => 0x03,
            Self::ReadInputRegisters => 0x04,
            Self::WriteMultipleCoils => 0x0F,
            Self::WriteMultipleRegisters => 0x10,
            Self::MaskWriteRegister => 0x16,
            Self::ReadWriteMultipleRegisters => 0x17,
            #[cfg(feature = "rtu")]
            Self::ReadExceptionStatus => 0x07,
            #[cfg(feature = "rtu")]
            Self::Diagnostics => 0x08,
            #[cfg(feature = "rtu")]
            Self::GetCommEventCounter => 0x0B,
            #[cfg(feature = "rtu")]
            Self::GetCommEventLog => 0x0C,
            #[cfg(feature = "rtu")]
            Self::ReportServerId => 0x11,
            Self::Custom(code) => code,
        }
    }
}

impl fmt::Display for FunctionCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.value().fmt(f)
    }
}

/// A Modbus sub-function code is represented by an unsigned 16 bit integer.
#[cfg(feature = "rtu")]
pub(crate) type SubFunctionCode = u16;

/// A Modbus address is represented by 16 bit (from `0` to `65535`).
pub(crate) type Address = u16;

/// A Coil represents a single bit.
///
/// - `true` is equivalent to `ON`, `1` and `0xFF00`.
/// - `false` is equivalent to `OFF`, `0` and `0x0000`.
pub(crate) type Coil = bool;

/// Modbus uses 16 bit for its data items (big-endian representation).
pub(crate) type Word = u16;

/// Number of items to process (`0` - `65535`).
pub(crate) type Quantity = u16;

/// Raw PDU data
type RawData<'r> = &'r [u8];

/// A request represents a message from the client (master) to the server (slave).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Request<'r> {
    ReadCoils(Address, Quantity),
    ReadDiscreteInputs(Address, Quantity),
    WriteSingleCoil(Address, Coil),
    WriteMultipleCoils(Address, Coils<'r>),
    ReadInputRegisters(Address, Quantity),
    ReadHoldingRegisters(Address, Quantity),
    WriteSingleRegister(Address, Word),
    WriteMultipleRegisters(Address, Data<'r>),
    ReadWriteMultipleRegisters(Address, Quantity, Address, Data<'r>),
    #[cfg(feature = "rtu")]
    ReadExceptionStatus,
    #[cfg(feature = "rtu")]
    Diagnostics(SubFunctionCode, Data<'r>),
    #[cfg(feature = "rtu")]
    GetCommEventCounter,
    #[cfg(feature = "rtu")]
    GetCommEventLog,
    #[cfg(feature = "rtu")]
    ReportServerId,
    //TODO:
    //- ReadFileRecord
    //- WriteFileRecord
    //- MaskWriteRegiger
    //TODO:
    //- Read FifoQueue
    //- EncapsulatedInterfaceTransport
    //- CanOpenGeneralReferenceRequestAndResponsePdu
    //- ReadDeviceIdentification
    Custom(FunctionCode, &'r [u8]),
}

/// A server (slave) exception response.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExceptionResponse {
    pub function: FunctionCode,
    pub exception: Exception,
}

/// Represents a message from the client (slave) to the server (master).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RequestPdu<'r>(pub Request<'r>);

/// Represents a message from the server (slave) to the client (master).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResponsePdu<'r>(pub Result<Response<'r>, ExceptionResponse>);

#[cfg(feature = "rtu")]
type Status = u16;
#[cfg(feature = "rtu")]
type EventCount = u16;
#[cfg(feature = "rtu")]
type MessageCount = u16;

/// The response data of a successfull request.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Response<'r> {
    ReadCoils(Coils<'r>),
    ReadDiscreteInputs(Coils<'r>),
    WriteSingleCoil(Address),
    WriteMultipleCoils(Address, Quantity),
    ReadInputRegisters(Data<'r>),
    ReadHoldingRegisters(Data<'r>),
    WriteSingleRegister(Address, Word),
    WriteMultipleRegisters(Address, Quantity),
    ReadWriteMultipleRegisters(Data<'r>),
    #[cfg(feature = "rtu")]
    ReadExceptionStatus(u8),
    #[cfg(feature = "rtu")]
    Diagnostics(Data<'r>),
    #[cfg(feature = "rtu")]
    GetCommEventCounter(Status, EventCount),
    #[cfg(feature = "rtu")]
    GetCommEventLog(Status, EventCount, MessageCount, &'r [u8]),
    #[cfg(feature = "rtu")]
    ReportServerId(&'r [u8], bool),
    //TODO:
    //- ReadFileRecord
    //- WriteFileRecord
    //- MaskWriteRegiger
    //TODO:
    //- Read FifoQueue
    //- EncapsulatedInterfaceTransport
    //- CanOpenGeneralReferenceRequestAndResponsePdu
    //- ReadDeviceIdentification
    Custom(FunctionCode, &'r [u8]),
}

impl<'r> From<Request<'r>> for FunctionCode {
    fn from(r: Request<'r>) -> Self {
        use Request as R;

        match r {
            R::ReadCoils(_, _) => Self::ReadCoils,
            R::ReadDiscreteInputs(_, _) => Self::ReadDiscreteInputs,
            R::WriteSingleCoil(_, _) => Self::WriteSingleCoil,
            R::WriteMultipleCoils(_, _) => Self::WriteMultipleCoils,
            R::ReadInputRegisters(_, _) => Self::ReadInputRegisters,
            R::ReadHoldingRegisters(_, _) => Self::ReadHoldingRegisters,
            R::WriteSingleRegister(_, _) => Self::WriteSingleRegister,
            R::WriteMultipleRegisters(_, _) => Self::WriteMultipleRegisters,
            R::ReadWriteMultipleRegisters(_, _, _, _) => Self::ReadWriteMultipleRegisters,
            #[cfg(feature = "rtu")]
            R::ReadExceptionStatus => Self::ReadExceptionStatus,
            #[cfg(feature = "rtu")]
            R::Diagnostics(_, _) => Self::Diagnostics,
            #[cfg(feature = "rtu")]
            R::GetCommEventCounter => Self::GetCommEventCounter,
            #[cfg(feature = "rtu")]
            R::GetCommEventLog => Self::GetCommEventLog,
            #[cfg(feature = "rtu")]
            R::ReportServerId => Self::ReportServerId,
            R::Custom(code, _) => code,
        }
    }
}

impl<'r> From<Response<'r>> for FunctionCode {
    fn from(r: Response<'r>) -> Self {
        use Response as R;

        match r {
            R::ReadCoils(_) => Self::ReadCoils,
            R::ReadDiscreteInputs(_) => Self::ReadDiscreteInputs,
            R::WriteSingleCoil(_) => Self::WriteSingleCoil,
            R::WriteMultipleCoils(_, _) => Self::WriteMultipleCoils,
            R::ReadInputRegisters(_) => Self::ReadInputRegisters,
            R::ReadHoldingRegisters(_) => Self::ReadHoldingRegisters,
            R::WriteSingleRegister(_, _) => Self::WriteSingleRegister,
            R::WriteMultipleRegisters(_, _) => Self::WriteMultipleRegisters,
            R::ReadWriteMultipleRegisters(_) => Self::ReadWriteMultipleRegisters,
            #[cfg(feature = "rtu")]
            R::ReadExceptionStatus(_) => Self::ReadExceptionStatus,
            #[cfg(feature = "rtu")]
            R::Diagnostics(_) => Self::Diagnostics,
            #[cfg(feature = "rtu")]
            R::GetCommEventCounter(_, _) => Self::GetCommEventCounter,
            #[cfg(feature = "rtu")]
            R::GetCommEventLog(_, _, _, _) => Self::GetCommEventLog,
            #[cfg(feature = "rtu")]
            R::ReportServerId(_, _) => Self::ReportServerId,
            R::Custom(code, _) => code,
        }
    }
}

/// A server (slave) exception.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Exception {
    IllegalFunction = 0x01,
    IllegalDataAddress = 0x02,
    IllegalDataValue = 0x03,
    ServerDeviceFailure = 0x04,
    Acknowledge = 0x05,
    ServerDeviceBusy = 0x06,
    MemoryParityError = 0x08,
    GatewayPathUnavailable = 0x0A,
    GatewayTargetDevice = 0x0B,
}

impl fmt::Display for Exception {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let desc = match *self {
            Self::IllegalFunction => "Illegal function",
            Self::IllegalDataAddress => "Illegal data address",
            Self::IllegalDataValue => "Illegal data value",
            Self::ServerDeviceFailure => "Server device failure",
            Self::Acknowledge => "Acknowledge",
            Self::ServerDeviceBusy => "Server device busy",
            Self::MemoryParityError => "Memory parity error",
            Self::GatewayPathUnavailable => "Gateway path unavailable",
            Self::GatewayTargetDevice => "Gateway target device failed to respond",
        };
        write!(f, "{desc}")
    }
}

impl<'r> Request<'r> {
    /// Number of bytes required for a serialized PDU frame.
    #[must_use]
    pub fn pdu_len(&self) -> usize {
        match *self {
            Self::ReadCoils(_, _)
            | Self::ReadDiscreteInputs(_, _)
            | Self::ReadInputRegisters(_, _)
            | Self::ReadHoldingRegisters(_, _)
            | Self::WriteSingleRegister(_, _)
            | Self::WriteSingleCoil(_, _) => 5,
            Self::WriteMultipleCoils(_, coils) => 6 + coils.packed_len(),
            Self::WriteMultipleRegisters(_, words) => 6 + words.data.len(),
            Self::ReadWriteMultipleRegisters(_, _, _, words) => 10 + words.data.len(),
            Self::Custom(_, data) => 1 + data.len(),
            #[cfg(feature = "rtu")]
            _ => todo!(), // TODO
        }
    }
}

impl<'r> Response<'r> {
    /// Number of bytes required for a serialized PDU frame.
    #[must_use]
    pub fn pdu_len(&self) -> usize {
        match *self {
            Self::ReadCoils(coils) | Self::ReadDiscreteInputs(coils) => 2 + coils.packed_len(),
            Self::WriteSingleCoil(_) => 3,
            Self::WriteMultipleCoils(_, _)
            | Self::WriteMultipleRegisters(_, _)
            | Self::WriteSingleRegister(_, _) => 5,
            Self::ReadInputRegisters(words)
            | Self::ReadHoldingRegisters(words)
            | Self::ReadWriteMultipleRegisters(words) => 2 + words.len() * 2,
            Self::Custom(_, data) => 1 + data.len(),
            #[cfg(feature = "rtu")]
            _ => unimplemented!(), // TODO
        }
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn function_code_into_u8() {
        let x: u8 = FunctionCode::WriteMultipleCoils.value();
        assert_eq!(x, 15);
        let x: u8 = FunctionCode::Custom(0xBB).value();
        assert_eq!(x, 0xBB);
    }

    #[test]
    fn function_code_from_u8() {
        assert_eq!(FunctionCode::new(15), FunctionCode::WriteMultipleCoils);
        assert_eq!(FunctionCode::new(0xBB), FunctionCode::Custom(0xBB));
    }

    #[test]
    fn function_code_from_request() {
        use Request::*;
        let requests = &[
            (ReadCoils(0, 0), 1),
            (ReadDiscreteInputs(0, 0), 2),
            (WriteSingleCoil(0, true), 5),
            (
                WriteMultipleCoils(
                    0,
                    Coils {
                        quantity: 0,
                        data: &[],
                    },
                ),
                0x0F,
            ),
            (ReadInputRegisters(0, 0), 0x04),
            (ReadHoldingRegisters(0, 0), 0x03),
            (WriteSingleRegister(0, 0), 0x06),
            (
                WriteMultipleRegisters(
                    0,
                    Data {
                        quantity: 0,
                        data: &[],
                    },
                ),
                0x10,
            ),
            (
                ReadWriteMultipleRegisters(
                    0,
                    0,
                    0,
                    Data {
                        quantity: 0,
                        data: &[],
                    },
                ),
                0x17,
            ),
            (Custom(FunctionCode::Custom(88), &[]), 88),
        ];
        for (req, expected) in requests {
            let code: u8 = FunctionCode::from(*req).value();
            assert_eq!(*expected, code);
        }
    }

    #[test]
    fn function_code_from_response() {
        use Response::*;
        let responses = &[
            (
                ReadCoils(Coils {
                    quantity: 0,
                    data: &[],
                }),
                1,
            ),
            (
                ReadDiscreteInputs(Coils {
                    quantity: 0,
                    data: &[],
                }),
                2,
            ),
            (WriteSingleCoil(0x0), 5),
            (WriteMultipleCoils(0x0, 0x0), 0x0F),
            (
                ReadInputRegisters(Data {
                    quantity: 0,
                    data: &[],
                }),
                0x04,
            ),
            (
                ReadHoldingRegisters(Data {
                    quantity: 0,
                    data: &[],
                }),
                0x03,
            ),
            (WriteSingleRegister(0, 0), 0x06),
            (WriteMultipleRegisters(0, 0), 0x10),
            (
                ReadWriteMultipleRegisters(Data {
                    quantity: 0,
                    data: &[],
                }),
                0x17,
            ),
            (Custom(FunctionCode::Custom(99), &[]), 99),
        ];
        for (req, expected) in responses {
            let code: u8 = FunctionCode::from(*req).value();
            assert_eq!(*expected, code);
        }
    }

    #[test]
    fn test_request_pdu_len() {
        assert_eq!(Request::ReadCoils(0x12, 5).pdu_len(), 5);
        assert_eq!(Request::WriteSingleRegister(0x12, 0x33).pdu_len(), 5);
        let buf = &mut [0, 0];
        assert_eq!(
            Request::WriteMultipleCoils(0, Coils::from_bools(&[true, false], buf).unwrap())
                .pdu_len(),
            7
        );
        // TODO: extend test
    }

    #[test]
    fn test_response_pdu_len() {
        let buf = &mut [0, 0];
        assert_eq!(
            Response::ReadCoils(Coils::from_bools(&[true], buf).unwrap()).pdu_len(),
            3
        );
        // TODO: extend test
    }
}