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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
use crate::args::*;
use crate::error::MessageParseError;

/// Represents the types of messages that are specified by the model railroads protocol.
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Message {
    /// Forces the model railroads to switch in Idle state. An emergency stop for all trains is broadcast.
    /// Note: The model railroads may not response any more.
    Idle,
    /// Turns the global power on. Activates the railway.
    GpOn,
    /// Turns the global power off. Deactivates the railway.
    GpOff,
    /// This is the master `Busy` code. Receiving this indicates that
    /// the master needs time to fulfill a send request.
    Busy,

    /// Requests a loco address to be put to a free slot by the master.
    ///
    /// Using this slot the train is controllable.
    ///
    /// # Success
    ///
    /// [`Message::SlRdData`] containing all slot and address information.
    ///
    /// # Fail
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::failed()`]
    /// Meaning no free slots are available.
    LocoAdr(AddressArg),
    /// Request state of switch with acknowledgment function
    ///
    /// # Success
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::success()`]
    ///
    /// # Fail
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::failed()`]
    /// Meaning switch state not known / Switch not known.
    SwAck(SwitchArg),
    /// Request state of switch
    ///
    /// # Success
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::success()`]
    ///
    /// # Fail
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::failed()`]
    /// Meaning switch state not known / Switch not known.
    SwState(SwitchArg),
    /// Request slot data or status block
    ///
    /// # Success
    ///
    /// [`Message::SlRdData`] containing all slot information.
    RqSlData(SlotArg),
    /// Moves all slot information from a `source` to a `destination` slot address.
    ///
    /// # Special operations
    ///
    /// ## `NULL`-Move
    ///
    /// A `NULL`-Move (Move with equal source and destination) can be used to mark the slot as [`State::InUse`].
    ///
    /// ## Dispatch put
    ///
    /// Moving a slot **to** the *slot 0* marks it as `DISPATCH`-Slot.
    /// In this case the destination slot is not copied to,
    /// but marked by the system as DISPATCH slot.
    ///
    /// ## Dispatch get
    ///
    /// Moving a slot **from** *slot 0* with no destination given (not needed) will
    /// response with a [`Message::SlRdData`] if a as dispatch marked slot is saved in *slot 0*.
    /// Otherwise a [`Message::LongAck`] with [`Ack1Arg::failed()`] indicates the
    /// failure of the operation.
    ///
    /// # Success
    ///
    /// [`Message::SlRdData`] containing all slot information.
    ///
    /// # Fail
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::failed()`]
    /// Meaning slot data could not be moved.
    MoveSlots(SlotArg, SlotArg),
    /// Links the first given slot to the second one
    ///
    /// # Success
    ///
    /// [`Message::SlRdData`] containing slot information.
    ///
    /// # Fail
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::failed()`]
    /// Meaning slot data could not be linked.
    LinkSlots(SlotArg, SlotArg),
    /// Unlinks the first given slot from the second one
    ///
    /// # Success
    ///
    /// [`Message::SlRdData`] containing slot information.
    ///
    /// # Fail
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::failed()`]
    /// Meaning slot data could not be linked.
    UnlinkSlots(SlotArg, SlotArg),
    /// Sets function bits in a [`Consist::LogicalMid`] or
    /// [`Consist::LogicalSubMember`] linked slot.
    ConsistFunc(SlotArg, DirfArg),
    /// Writes a slots stat1.
    /// See [`Stat1Arg`] for information on what you can configure here.
    SlotStat1(SlotArg, Stat1Arg),
    /// This is a long acknowledgment message mostly used to indicate that some requested
    /// operation has failed or succeed.
    LongAck(LopcArg, Ack1Arg),
    /// This holds general sensor input from an addressed sensor.
    ///
    /// On state change this message is automatically send from the sensor over the model railroads,
    /// but if you want to receive all your sensor states you can configure a switch address that
    /// forces the sensor module to send its state in the model railroads sensor device.
    InputRep(InArg),
    /// Switch sensor report
    ///
    /// Reports the switches type and meta information
    SwRep(SnArg),
    /// Requests a switch function. More precisely requests a switch to switch to a
    /// specific direction and activation.
    ///
    /// # Fail
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::failed()`]
    /// Meaning the requested action could not be performed.
    SwReq(SwitchArg),
    /// Sets a slots sound function bits. (functions 5 to 8)
    LocoSnd(SlotArg, SndArg),
    /// Sets a slots direction and first four function bits.
    LocoDirf(SlotArg, DirfArg),
    /// Sets a slot speed.
    LocoSpd(SlotArg, SpeedArg),

    /// Used for power management and transponding
    MultiSense(MultiSenseArg, AddressArg),
    /// In systems from `Uhlenbrock` this message could be used to
    /// access the slot functions 9 to 28.
    UhliFun(SlotArg, FunctionArg),

    /// Used to write special and more complex slot data.
    ///
    /// # Success
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::success()`]
    ///
    /// # Fail
    ///
    /// [`Message::LongAck`] with [`Ack1Arg::failed()`]
    WrSlData(WrSlDataStructure),
    /// This is a slot data response holding all information on the slot.
    SlRdData(
        SlotArg,
        Stat1Arg,
        AddressArg,
        SpeedArg,
        DirfArg,
        TrkArg,
        Stat2Arg,
        SndArg,
        IdArg,
    ),
    /// Holds a SlRdData response of the programming slot 127
    ///
    /// The first arguments represent this message in the format of a SlRdData response,
    /// but some data where extra interpreted in the below message arguments.
    ProgrammingFinalResponse(
        SlotArg,
        Stat1Arg,
        AddressArg,
        SpeedArg,
        DirfArg,
        TrkArg,
        Stat2Arg,
        SndArg,
        IdArg,
        Pcmd,
        PStat,
        AddressArg,
        CvDataArg,
    ),
    /// Indicates that the programming service mode is aborted.
    ProgrammingAborted(ProgrammingAbortedArg),

    /// Moves 8 bytes peer to peer from the source slot to the destination.
    ///
    /// Slot = 0: Slot is master
    /// Slot = 0x70 - 0x7E: reserved
    /// Slot = 0x7F: Throttle message xfer
    ///
    PeerXfer(SlotArg, DstArg, PxctData),

    /// This message holds reports
    /// (I am not really sure what this reports represent
    /// and what they are used for.
    /// If you understand them better,
    /// you may help me to improve this documentation and
    /// the implementation of reading and writing this messages.)
    Rep(RepStructure),

    /// Sends n-byte packet immediate
    ///
    /// # Response
    ///
    /// - [`Message::LongAck`] with [`Ack1Arg::success()`]: Not limited
    /// - [`Message::LongAck`] with [`Ack1Arg::limited_success()`]:
    ///   limited with [`Ack1Arg::ack1()`] as limit
    /// - [`Message::LongAck`] with [`Ack1Arg::failed()`]: Busy
    ImmPacket(ImArg),
}

impl Message {
    /// Parses a model railroads message from `buf`.
    ///
    /// # Errors
    ///
    /// This function returns an error if the message could not be parsed:
    ///
    /// - [`UnknownOpcode`]: If the message has an unknown opcode
    /// - [`UnexpectedEnd`]: If the buf not holds the complete message
    /// - [`InvalidChecksum`]: If the checksum is invalid
    /// - [`InvalidFormat`]: If the message is in invalid format
    ///
    /// [`UnknownOpcode`]: MessageParseError::UnknownOpcode
    /// [`UnexpectedEnd`]: MessageParseError::UnexpectedEnd
    /// [`InvalidChecksum`]: MessageParseError::InvalidChecksum
    /// [`InvalidFormat`]: MessageParseError::InvalidFormat
    pub fn parse(buf: &[u8]) -> Result<Self, MessageParseError> {
        let opc = buf[0];
        // We calculate the length of the remaining message to read
        let len = match opc & 0xE0 {
            0x80 => 2,
            0xA0 => 4,
            0xC0 => 6,
            0xE0 => buf[1] as usize,
            _ => return Err(MessageParseError::UnknownOpcode(opc)),
        };

        // validate checksum
        if !Self::validate(&buf[0..len]) {
            return Err(MessageParseError::InvalidChecksum(opc));
        }

        // call appropriate parse function
        match len {
            2 => Self::parse2(opc),
            4 => Self::parse4(opc, &buf[1..3]),
            6 => Self::parse6(opc, &buf[1..5]),
            var => Self::parse_var(opc, &buf[1..var - 1]),
        }
    }

    /// Parse all messages of two bytes length. As the second byte is every time the checksum,
    /// only the `opc` is needed for parsing.
    ///
    /// # Errors
    ///
    /// - [`UnknownOpcode`]: If the message has an unknown opcode
    ///
    /// [`UnknownOpcode`]: MessageParseError::UnknownOpcode
    fn parse2(opc: u8) -> Result<Self, MessageParseError> {
        match opc {
            0x85 => Ok(Self::Idle),
            0x83 => Ok(Self::GpOn),
            0x82 => Ok(Self::GpOff),
            0x81 => Ok(Self::Busy),
            _ => Err(MessageParseError::UnknownOpcode(opc)),
        }
    }

    /// Parse all messages of four bytes length.
    /// Therefore the first byte specifying the message type is passed as `opc` and the
    /// other two message bytes are passed as `args`.
    ///
    /// # Errors
    ///
    /// - [`UnknownOpcode`]: If the message has an unknown opcode
    /// - [`UnexpectedEnd`]: If the buf not holds the complete message
    ///
    /// [`UnknownOpcode`]: MessageParseError::UnknownOpcode
    /// [`UnexpectedEnd`]: MessageParseError::UnexpectedEnd
    fn parse4(opc: u8, args: &[u8]) -> Result<Self, MessageParseError> {
        if args.len() != 2 {
            return Err(MessageParseError::UnexpectedEnd(opc));
        }
        match opc {
            0xBF => Ok(Self::LocoAdr(AddressArg::parse(args[0], args[1]))),
            0xBD => Ok(Self::SwAck(SwitchArg::parse(args[0], args[1]))),
            0xBC => Ok(Self::SwState(SwitchArg::parse(args[0], args[1]))),
            0xBB => Ok(Self::RqSlData(SlotArg::parse(args[0]))),
            0xBA => Ok(Self::MoveSlots(
                SlotArg::parse(args[0]),
                SlotArg::parse(args[1]),
            )),
            0xB9 => Ok(Self::LinkSlots(
                SlotArg::parse(args[0]),
                SlotArg::parse(args[1]),
            )),
            0xB8 => Ok(Self::UnlinkSlots(
                SlotArg::parse(args[0]),
                SlotArg::parse(args[1]),
            )),
            0xB6 => Ok(Self::ConsistFunc(
                SlotArg::parse(args[0]),
                DirfArg::parse(args[1]),
            )),
            0xB5 => Ok(Self::SlotStat1(
                SlotArg::parse(args[0]),
                Stat1Arg::parse(args[1]),
            )),
            0xB4 => Ok(Self::LongAck(
                LopcArg::parse(args[0]),
                Ack1Arg::parse(args[1]),
            )),
            0xB2 => Ok(Self::InputRep(InArg::parse(args[0], args[1]))),
            0xB1 => Ok(Self::SwRep(SnArg::parse(args[0], args[1]))),
            0xB0 => Ok(Self::SwReq(SwitchArg::parse(args[0], args[1]))),
            0xA2 => Ok(Self::LocoSnd(
                SlotArg::parse(args[0]),
                SndArg::parse(args[1]),
            )),
            0xA1 => Ok(Self::LocoDirf(
                SlotArg::parse(args[0]),
                DirfArg::parse(args[1]),
            )),
            0xA0 => Ok(Self::LocoSpd(
                SlotArg::parse(args[0]),
                SpeedArg::parse(args[1]),
            )),
            _ => Err(MessageParseError::UnknownOpcode(opc)),
        }
    }

    /// Parse all messages of six bytes length.
    /// Therefore the first byte specifying the message type is passed as `opc` and the
    /// other four message bytes are passed as `args`.
    ///
    /// # Errors
    ///
    /// - [`UnknownOpcode`]: If the message has an unknown opcode
    /// - [`UnexpectedEnd`]: If the buf not holds the complete message
    /// - [`InvalidFormat`]: If the message is in invalid format
    ///
    /// [`UnknownOpcode`]: MessageParseError::UnknownOpcode
    /// [`UnexpectedEnd`]: MessageParseError::UnexpectedEnd
    /// [`InvalidFormat`]: MessageParseError::InvalidFormat
    fn parse6(opc: u8, args: &[u8]) -> Result<Self, MessageParseError> {
        if args.len() != 4 {
            return Err(MessageParseError::UnexpectedEnd(opc));
        }
        match opc {
            0xD0 => Ok(Self::MultiSense(
                MultiSenseArg::parse(args[0], args[1]),
                AddressArg::parse(args[2], args[3]),
            )),
            0xD4 => {
                if 0x20 != args[0] {
                    return Err(MessageParseError::InvalidFormat(format!(
                        "Expected first arg of UhliFun to be 0x20 got {:02x}",
                        args[0]
                    )));
                }
                Ok(Self::UhliFun(
                    SlotArg::parse(args[1]),
                    FunctionArg::parse(args[2], args[3]),
                ))
            }
            _ => Err(MessageParseError::UnknownOpcode(opc)),
        }
    }

    /// Parse all messages of variable length.
    /// Therefore the first byte specifying the message type is passed as `opc` and the
    /// other message bytes are passed as `args`.
    ///
    /// # Errors
    ///
    /// - [`UnknownOpcode`]: If the message has an unknown opcode
    /// - [`UnexpectedEnd`]: If the buf not holds the complete message
    /// - [`InvalidFormat`]: If the message is in invalid format
    ///
    /// [`UnknownOpcode`]: MessageParseError::UnknownOpcode
    /// [`UnexpectedEnd`]: MessageParseError::UnexpectedEnd
    /// [`InvalidFormat`]: MessageParseError::InvalidFormat
    fn parse_var(opc: u8, args: &[u8]) -> Result<Self, MessageParseError> {
        if args.len() + 2 != args[0] as usize {
            return Err(MessageParseError::UnexpectedEnd(opc));
        }

        match opc {
            0xED => {
                if args.len() != 9 {
                    return Err(MessageParseError::UnexpectedEnd(opc));
                }

                if args[1] != 0x7F {
                    return Err(MessageParseError::InvalidFormat(format!(
                        "The check byte of the received message whith opcode {:x} was invalid. \
                            Expected 0x7F got {:02x}",
                        opc, args[1]
                    )));
                }

                Ok(Self::ImmPacket(ImArg::parse(
                    args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8],
                )))
            }
            0xEF => {
                if args.len() != 12 {
                    return Err(MessageParseError::UnexpectedEnd(opc));
                }

                Ok(Self::WrSlData(WrSlDataStructure::parse(
                    args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8],
                    args[9], args[10], args[11],
                )))
            }
            0xE7 => {
                if args.len() != 12 {
                    return Err(MessageParseError::UnexpectedEnd(opc));
                }

                if args[1] == 0x7C {
                    Ok(Self::ProgrammingFinalResponse(
                        SlotArg::parse(args[1]),
                        Stat1Arg::parse(args[2]),
                        AddressArg::parse(args[8], args[3]),
                        SpeedArg::parse(args[4]),
                        DirfArg::parse(args[5]),
                        TrkArg::parse(args[6]),
                        Stat2Arg::parse(args[7]),
                        SndArg::parse(args[9]),
                        IdArg::parse(args[10], args[11]),
                        Pcmd::parse(args[2]),
                        PStat::parse(args[3]),
                        AddressArg::parse(args[4], args[5]),
                        CvDataArg::parse(args[7], args[8], args[9]),
                    ))
                } else {
                    Ok(Self::SlRdData(
                        SlotArg::parse(args[1]),
                        Stat1Arg::parse(args[2]),
                        AddressArg::parse(args[8], args[3]),
                        SpeedArg::parse(args[4]),
                        DirfArg::parse(args[5]),
                        TrkArg::parse(args[6]),
                        Stat2Arg::parse(args[7]),
                        SndArg::parse(args[9]),
                        IdArg::parse(args[10], args[11]),
                    ))
                }
            }
            0xE6 => {
                if args.len() < 2 {
                    return Err(MessageParseError::UnexpectedEnd(opc));
                }

                Ok(Message::ProgrammingAborted(ProgrammingAbortedArg::parse(
                    args[0],
                    &args[1..],
                )))
            },
            0xE4 => {
                if args.len() < 2 {
                    return Err(MessageParseError::UnexpectedEnd(opc));
                }

                Ok(Self::Rep(match RepStructure::parse(args[0], &args[1..]) {
                    Err(err) => return Err(err),
                    Ok(rep) => rep,
                }))
            },
            0xE5 => {
                if args.len() != 14 {
                    return Err(MessageParseError::UnexpectedEnd(opc));
                }

                Ok(Self::PeerXfer(
                    SlotArg::parse(args[1]),
                    DstArg::parse(args[2], args[3]),
                    PxctData::parse(
                        args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11],
                        args[12], args[13],
                    ),
                ))
            }
            _ => Err(MessageParseError::UnknownOpcode(opc)),
        }
    }

    /// Validates the `msg` by xor-ing all bytes and checking for the result to be 0xFF.
    fn validate(msg: &[u8]) -> bool {
        return msg.iter().fold(0, |acc, &b| acc ^ b) == 0xFF;
    }

    /// Parses the given [`Message`] to a [`Vec<u8>`] using the model railroads protocol.
    pub fn to_message(self) -> Vec<u8> {
        // Parses the message
        let mut message = match self {
            Message::Idle => vec![0x85_u8],
            Message::GpOn => vec![0x83_u8],
            Message::GpOff => vec![0x82_u8],
            Message::Busy => vec![0x81_u8],
            Message::LocoAdr(adr_arg) => vec![0xBF_u8, adr_arg.adr2(), adr_arg.adr1()],
            Message::SwAck(switch_arg) => vec![0xBD_u8, switch_arg.sw1(), switch_arg.sw2()],
            Message::SwState(switch_arg) => vec![0xBC_u8, switch_arg.sw1(), switch_arg.sw2()],
            Message::RqSlData(slot_arg) => vec![0xBB_u8, slot_arg.slot(), 0x00_u8],
            Message::MoveSlots(src, dst) => vec![0xBA_u8, src.slot(), dst.slot()],
            Message::LinkSlots(sl1, sl2) => vec![0xB9_u8, sl1.slot(), sl2.slot()],
            Message::UnlinkSlots(sl1, sl2) => vec![0xB8_u8, sl1.slot(), sl2.slot()],
            Message::ConsistFunc(slot, dirf) => vec![0xB6_u8, slot.slot(), dirf.dirf()],
            Message::SlotStat1(slot, stat1) => vec![0xB5_u8, slot.slot(), stat1.stat1()],
            Message::LongAck(lopc, ack1) => vec![0xB4_u8, lopc.lopc(), ack1.ack1()],
            Message::InputRep(input) => vec![0xB2_u8, input.in1(), input.in2()],
            Message::SwRep(sn_arg) => vec![0xB1_u8, sn_arg.sn1(), sn_arg.sn2()],
            Message::SwReq(sw) => vec![0xB0_u8, sw.sw1(), sw.sw2()],
            Message::LocoSnd(slot, snd) => vec![0xA2_u8, slot.slot(), snd.snd()],
            Message::LocoDirf(slot, dirf) => vec![0xA1_u8, slot.slot(), dirf.dirf()],
            Message::LocoSpd(slot, spd) => vec![0xA0_u8, slot.slot(), spd.spd()],
            Message::MultiSense(multi_sense, address) => vec![
                0xD0_u8,
                multi_sense.m_high(),
                multi_sense.zas(),
                address.adr2(),
                address.adr1(),
            ],
            Message::UhliFun(slot, function) => vec![
                0xD4_u8,
                0x20_u8,
                slot.slot(),
                function.group(),
                function.function(),
            ],
            Message::WrSlData(wr_slot_data_arg) => wr_slot_data_arg.to_message(),
            Message::SlRdData(slot, stat1, adr, spd, dirf, trk, stat2, snd, id) => vec![
                0xE7_u8,
                0x0E_u8,
                slot.slot(),
                stat1.stat1(),
                adr.adr1(),
                spd.spd(),
                dirf.dirf(),
                trk.trk_arg(),
                stat2.stat2(),
                adr.adr2(),
                snd.snd(),
                id.id1(),
                id.id2(),
            ],
            Message::ProgrammingFinalResponse(
                slot,
                stat1,
                adr,
                spd,
                dirf,
                trk,
                stat2,
                snd,
                id,
                pcmd,
                stat,
                opsa,
                cv_data,
            ) => vec![
                0xE7_u8,
                0x0E_u8,
                slot.slot(),
                stat1.stat1() | pcmd.pcmd(),
                adr.adr1() | stat.stat(),
                spd.spd() | opsa.adr2(),
                dirf.dirf() | opsa.adr1(),
                trk.trk_arg(),
                stat2.stat2() | cv_data.cvh(),
                adr.adr2() | cv_data.cvl(),
                snd.snd() | cv_data.data7(),
                id.id1(),
                id.id2(),
            ],
            Message::ProgrammingAborted(args) => args.to_message(),
            Message::ImmPacket(im) => vec![
                0xED_u8,
                0x0B_u8,
                0x7F_u8,
                im.reps(),
                im.dhi(),
                im.im1(),
                im.im2(),
                im.im3(),
                im.im4(),
                im.im5(),
            ],
            Message::Rep(rep) => match rep {
                RepStructure::RFID7Report(report) => report.to_message(),
                RepStructure::RFID5Report(report) => report.to_message(),
                RepStructure::LissyIrReport(report) => report.to_message(),
                RepStructure::WheelcntReport(report) => report.to_message(),
            },
            Message::PeerXfer(src, dst, pxct) => vec![
                0xE5,
                0x10,
                src.slot(),
                dst.dst_low(),
                dst.dst_high(),
                pxct.pxct1(),
                pxct.d1(),
                pxct.d2(),
                pxct.d3(),
                pxct.d4(),
                pxct.pxct2(),
                pxct.d5(),
                pxct.d6(),
                pxct.d7(),
                pxct.d8(),
            ],
        };

        // Appending checksum to the created message
        message.push(Self::check_sum(&message));

        message
    }

    /// Calculates the check sum for the given `msg`.
    fn check_sum(msg: &[u8]) -> u8 {
        0xFF - msg.iter().fold(0, |acc, &b| acc ^ b)
    }

    /// Checks whether the given operation code is a valid
    /// known operation code that could be passed by this protocol implementation.
    ///
    /// # Parameters
    ///
    /// - `opc`: The operation code to check
    ///
    /// # Returns
    ///
    /// If the given operation code is known
    pub fn known_opc(opc: u8) -> bool {
        matches!(
            opc,
            0x85 | 0x83
                | 0x82
                | 0x81
                | 0xBF
                | 0xBD
                | 0xBC
                | 0xBB
                | 0xBA
                | 0xB9
                | 0xB8
                | 0xB6
                | 0xB5
                | 0xB4
                | 0xB2
                | 0xB1
                | 0xB0
                | 0xA2
                | 0xA1
                | 0xA0
                | 0xD0
                | 0xD4
                | 0xEF
                | 0xE7
                | 0xE6
                | 0xE5
                | 0xE4
                | 0xED
        )
    }

    /// # Returns
    ///
    /// The op code for the specified message
    pub fn opc(&self) -> u8 {
        match *self {
            Message::Idle => 0x85,
            Message::GpOn => 0x83,
            Message::GpOff => 0x82,
            Message::Busy => 0x81,
            Message::LocoAdr(..) => 0xBF,
            Message::SwAck(..) => 0xBD,
            Message::SwState(..) => 0xBC,
            Message::RqSlData(..) => 0xBB,
            Message::MoveSlots(..) => 0xBA,
            Message::LinkSlots(..) => 0xB9,
            Message::UnlinkSlots(..) => 0xB8,
            Message::ConsistFunc(..) => 0xB6,
            Message::SlotStat1(..) => 0xB5,
            Message::LongAck(..) => 0xB4,
            Message::InputRep(..) => 0xB2,
            Message::SwRep(..) => 0xB1,
            Message::SwReq(..) => 0xB0,
            Message::LocoSnd(..) => 0xA2,
            Message::LocoDirf(..) => 0xA1,
            Message::LocoSpd(..) => 0xA0,
            Message::MultiSense(..) => 0xD0,
            Message::UhliFun(..) => 0xD4,
            Message::WrSlData(..) => 0xEF,
            Message::SlRdData(..) => 0xE7,
            Message::ProgrammingFinalResponse(..) => 0xE7,
            Message::ProgrammingAborted(..) => 0xE6,
            Message::PeerXfer(..) => 0xE5,
            Message::Rep(..) => 0xE4,
            Message::ImmPacket(..) => 0xED,
        }
    }

    /// Checks whether this message expects a long acknowledgment message to follow.
    pub fn answer_follows(&self) -> bool {
        0x01 & self.opc() == 0x01
    }

    /// Indicates if a request with the specified slot
    /// data was awaited after that message.
    pub fn await_slot_data(&self) -> bool {
        matches!(
            self,
            Message::LocoAdr(..)
                | Message::RqSlData(..)
                | Message::MoveSlots(..)
                | Message::LinkSlots(..)
                | Message::UnlinkSlots(..)
        )
    }
}