dnp3 1.6.0

Rust implementation of DNP3 (IEEE 1815) with idiomatic bindings for C, C++, .NET, and Java
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
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
use crate::app::attr::{AttrSet, OwnedAttribute};
use std::ops::BitAnd;

use crate::app::control::CommandStatus;
use crate::app::format::write::HeaderWriter;
use crate::app::gen::prefixed::PrefixedVariation;
use crate::app::parse::count::CountSequence;
use crate::app::parse::parser::{HeaderCollection, HeaderDetails};
use crate::app::parse::prefix::Prefix;
use crate::app::parse::traits::{FixedSizeVariation, Index};
use crate::app::variations::*;
use crate::app::Timestamp;
use crate::app::Variation::Group0;
use crate::master::error::CommandResponseError;
use crate::master::TaskError;
use crate::outstation::FreezeInterval;

/// Controls how a command request is issued
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum CommandMode {
    /// Master will use the `DIRECT_OPERATE` function code in a single request/response
    DirectOperate,
    /// Master will use the `SELECT` function code followed by `OPERATE` in two pass request/response
    SelectBeforeOperate,
}

/// Controls which time synchronization procedure is used
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum TimeSyncProcedure {
    /// Master will use the LAN procedure: RECORD_CURRENT_TIME followed by WRITE g50v3
    Lan,
    /// Master will use the non-LAN procedure: DELAY_MEASUREMENT followed by WRITE g50v1
    NonLan,
}

/// struct recording which event classes are enabled
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct EventClasses {
    /// enable Class 1
    pub class1: bool,
    /// enable Class 2
    pub class2: bool,
    /// enable Class 3
    pub class3: bool,
}

/// struct recording which event classes and class 0 are enabled
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct Classes {
    /// enable class zero
    pub class0: bool,
    /// enabled event classes
    pub events: EventClasses,
}

/// struct representing a one-byte range scan
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct OneByteRangeScan {
    /// variation to READ
    pub variation: Variation,
    /// start address of the READ
    pub start: u8,
    /// stop address of the READ
    pub stop: u8,
}

/// struct representing a two-byte range scan
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct TwoByteRangeScan {
    /// variation to READ
    pub variation: Variation,
    /// start address of the READ
    pub start: u16,
    /// stop address of the READ
    pub stop: u16,
}

/// struct representing an "all objects" (QC = 0x06) scan
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct AllObjectsScan {
    /// variation to READ
    pub variation: Variation,
}

/// struct representing a one-byte limited count scan
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct OneByteLimitedCountScan {
    /// variation to READ
    pub variation: Variation,
    /// maximum number of events
    pub count: u8,
}

/// struct representing a two-byte limited count scan
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct TwoByteLimitedCountScan {
    /// variation to READ
    pub variation: Variation,
    /// maximum number of events
    pub count: u16,
}

/// Represents a single header in a WRITE request to modify dead-bands within the outstation
#[derive(Debug, Clone)]
pub struct DeadBandHeader {
    // hidden implementation
    pub(crate) inner: DeadBandHeaderVariants,
}

impl DeadBandHeader {
    /// Group 34 variation 1 with 8-bit index
    pub fn group34_var1_u8(dead_bands: Vec<(u8, u16)>) -> Self {
        Self {
            inner: DeadBandHeaderVariants::G34V1U8(
                dead_bands
                    .iter()
                    .map(|(i, v)| (Group34Var1 { value: *v }, *i))
                    .collect(),
            ),
        }
    }

    /// Group 34 variation 1 with 16-bit index
    pub fn group34_var1_u16(dead_bands: Vec<(u16, u16)>) -> Self {
        Self {
            inner: DeadBandHeaderVariants::G34V1U16(
                dead_bands
                    .iter()
                    .map(|(i, v)| (Group34Var1 { value: *v }, *i))
                    .collect(),
            ),
        }
    }

    /// Group 34 variation 2 with 8-bit index
    pub fn group34_var2_u8(dead_bands: Vec<(u8, u32)>) -> Self {
        Self {
            inner: DeadBandHeaderVariants::G34V2U8(
                dead_bands
                    .iter()
                    .map(|(i, v)| (Group34Var2 { value: *v }, *i))
                    .collect(),
            ),
        }
    }

    /// Group 34 variation 2 with 16-bit index
    pub fn group34_var2_u16(dead_bands: Vec<(u16, u32)>) -> Self {
        Self {
            inner: DeadBandHeaderVariants::G34V2U16(
                dead_bands
                    .iter()
                    .map(|(i, v)| (Group34Var2 { value: *v }, *i))
                    .collect(),
            ),
        }
    }

    /// Group 34 variation 3 with 8-bit index
    pub fn group34_var3_u8(dead_bands: Vec<(u8, f32)>) -> Self {
        Self {
            inner: DeadBandHeaderVariants::G34V3U8(
                dead_bands
                    .iter()
                    .map(|(i, v)| (Group34Var3 { value: *v }, *i))
                    .collect(),
            ),
        }
    }

    /// Group 34 variation 3 with 16-bit index
    pub fn group34_var3_u16(dead_bands: Vec<(u16, f32)>) -> Self {
        Self {
            inner: DeadBandHeaderVariants::G34V3U16(
                dead_bands
                    .iter()
                    .map(|(i, v)| (Group34Var3 { value: *v }, *i))
                    .collect(),
            ),
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) enum DeadBandHeaderVariants {
    /// Group 34 variation 1 with 8-bit index
    G34V1U8(Vec<(Group34Var1, u8)>),
    /// Group 34 variation 1 with 16-bit index
    G34V1U16(Vec<(Group34Var1, u16)>),
    /// Group 34 variation 2 with 8-bit index
    G34V2U8(Vec<(Group34Var2, u8)>),
    /// Group 34 variation 2 with 16-bit index
    G34V2U16(Vec<(Group34Var2, u16)>),
    /// Group 34 variation 3 with 8-bit index
    G34V3U8(Vec<(Group34Var3, u8)>),
    /// Group 34 variation 3 with 16-bit index
    G34V3U16(Vec<(Group34Var3, u16)>),
}

impl EventClasses {
    /// construct an `EventClasses` from its fields
    pub fn new(class1: bool, class2: bool, class3: bool) -> Self {
        Self {
            class1,
            class2,
            class3,
        }
    }

    /// test if any of the event classes are enabled
    pub fn any(self) -> bool {
        self.class1 || self.class2 || self.class3
    }

    /// construct an `EventClasses` with all three classes enabled
    pub const fn all() -> Self {
        Self {
            class1: true,
            class2: true,
            class3: true,
        }
    }

    /// construct an `EventClasses` with all three classes disabled
    pub const fn none() -> Self {
        Self {
            class1: false,
            class2: false,
            class3: false,
        }
    }

    pub(crate) fn write(self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        if self.class1 {
            writer.write_all_objects_header(Variation::Group60Var2)?;
        }
        if self.class2 {
            writer.write_all_objects_header(Variation::Group60Var3)?;
        }
        if self.class3 {
            writer.write_all_objects_header(Variation::Group60Var4)?;
        }
        Ok(())
    }
}

impl BitAnd for EventClasses {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        Self::new(
            self.class1 && rhs.class1,
            self.class2 && rhs.class2,
            self.class3 && rhs.class3,
        )
    }
}

impl Classes {
    /// construct a `Classes` from its fields
    pub const fn new(class0: bool, events: EventClasses) -> Self {
        Self { class0, events }
    }

    /// construct a `Classes` with everything enabled
    pub const fn all() -> Self {
        Self::new(true, EventClasses::all())
    }

    /// construct a `Classes` with all events, without class 0.
    pub fn class123() -> Self {
        Self::new(false, EventClasses::all())
    }

    /// construct a `Classes` with class 0 and no events
    pub fn class0() -> Self {
        Self::new(true, EventClasses::none())
    }

    /// construct a `Classes` with nothing enabled
    pub fn none() -> Self {
        Self::new(false, EventClasses::none())
    }

    /// test if any classes (0/1/2/3) are enabled
    pub(crate) fn any(&self) -> bool {
        self.class0 || self.events.any()
    }

    pub(crate) fn write(self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        self.events.write(writer)?;
        if self.class0 {
            writer.write_all_objects_header(Variation::Group60Var1)?;
        }
        Ok(())
    }
}

impl OneByteRangeScan {
    /// construct a `OneByteRangeScan` from its fields
    pub fn new(variation: Variation, start: u8, stop: u8) -> Self {
        Self {
            variation,
            start,
            stop,
        }
    }

    pub(crate) fn write(self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        writer.write_range_only(self.variation, self.start, self.stop)
    }
}

impl TwoByteRangeScan {
    /// construct a `TwoByteRangeScan` from its fields
    pub fn new(variation: Variation, start: u16, stop: u16) -> Self {
        Self {
            variation,
            start,
            stop,
        }
    }

    pub(crate) fn write(self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        writer.write_range_only(self.variation, self.start, self.stop)
    }
}

impl AllObjectsScan {
    /// construct an `AllObjectsScan` from the variation
    pub fn new(variation: Variation) -> Self {
        Self { variation }
    }

    pub(crate) fn write(self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        writer.write_all_objects_header(self.variation)
    }
}

impl OneByteLimitedCountScan {
    /// construct an [`OneByteLimitedCountScan`] from the variation
    pub fn new(variation: Variation, count: u8) -> Self {
        Self { variation, count }
    }

    pub(crate) fn write(self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        writer.write_limited_count(self.variation, self.count)
    }
}

impl TwoByteLimitedCountScan {
    /// construct an [`TwoByteLimitedCountScan`] from the variation
    pub fn new(variation: Variation, count: u16) -> Self {
        Self { variation, count }
    }

    pub(crate) fn write(self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        writer.write_limited_count(self.variation, self.count)
    }
}

/// Enum representing all of the allowed scan types
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ReadHeader {
    /// variant for one byte range scans
    Range8(OneByteRangeScan),
    /// variant for two byte range scans
    Range16(TwoByteRangeScan),
    /// variant for all objects scans
    AllObjects(AllObjectsScan),
    /// variant for one byte limited count
    LimitedCount8(OneByteLimitedCountScan),
    /// variant for two byte limited count
    LimitedCount16(TwoByteLimitedCountScan),
}

impl ReadHeader {
    /// construct a one byte range [`ReadHeader`]
    pub fn one_byte_range(variation: Variation, start: u8, stop: u8) -> Self {
        ReadHeader::Range8(OneByteRangeScan::new(variation, start, stop))
    }

    /// construct a two range [`ReadHeader`]
    pub fn two_byte_range(variation: Variation, start: u16, stop: u16) -> Self {
        ReadHeader::Range16(TwoByteRangeScan::new(variation, start, stop))
    }

    /// construct an all objects [`ReadHeader`]
    pub fn all_objects(variation: Variation) -> Self {
        ReadHeader::AllObjects(AllObjectsScan::new(variation))
    }

    /// construct a one byte limited count scan [`ReadHeader`]
    pub fn one_byte_limited_count(variation: Variation, count: u8) -> Self {
        ReadHeader::LimitedCount8(OneByteLimitedCountScan::new(variation, count))
    }

    /// construct a two byte limited count scan [`ReadHeader`]
    pub fn two_byte_limited_count(variation: Variation, count: u16) -> Self {
        ReadHeader::LimitedCount16(TwoByteLimitedCountScan::new(variation, count))
    }

    pub(crate) fn format(self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        match self {
            ReadHeader::Range8(scan) => scan.write(writer),
            ReadHeader::Range16(scan) => scan.write(writer),
            ReadHeader::AllObjects(scan) => scan.write(writer),
            ReadHeader::LimitedCount8(scan) => scan.write(writer),
            ReadHeader::LimitedCount16(scan) => scan.write(writer),
        }
    }
}

/// Builder for write requests that hides the underlying type
#[derive(Clone, Debug, Default)]
pub struct Headers {
    headers: Vec<Header>,
}

#[derive(Clone, Debug)]
enum Header {
    Read(ReadHeader),
    TimeAndInterval(FreezeInterval),
    Attribute(OwnedAttribute),
}

impl Header {
    pub(crate) fn format(&self, writer: &mut HeaderWriter) -> Result<(), TaskError> {
        match self {
            Header::Read(x) => {
                x.format(writer)?;
            }
            Header::TimeAndInterval(x) => {
                let g50v2: Group50Var2 = <FreezeInterval as Into<Group50Var2>>::into(*x);
                writer.write_count_of_one(g50v2)?;
            }
            Header::Attribute(x) => {
                writer.write_attribute(x)?;
            }
        }
        Ok(())
    }

    #[cfg(feature = "ffi")]
    pub(crate) fn to_read_header(&self) -> Option<ReadHeader> {
        match self {
            Header::Read(x) => Some(*x),
            Header::TimeAndInterval(_) => None,
            Header::Attribute(_) => None,
        }
    }
}

impl Headers {
    /// Construct an empty set of request headers
    pub fn new() -> Self {
        Default::default()
    }

    /// Convert this generic request into a ReadRequest dropping irrelevant headers
    #[cfg(feature = "ffi")]
    pub fn to_read_request(&self) -> ReadRequest {
        ReadRequest::MultipleHeader(
            self.headers
                .iter()
                .filter_map(|x| x.to_read_header())
                .collect(),
        )
    }

    /// Add a header to the collection
    #[cfg(feature = "ffi")]
    pub fn push_read_header(&mut self, header: ReadHeader) {
        self.headers.push(header.into());
    }

    /// Add a header to the collection
    #[cfg(feature = "ffi")]
    pub fn push_freeze_interval(&mut self, interval: FreezeInterval) {
        self.headers.push(Header::TimeAndInterval(interval));
    }

    /// Add an attribute header to the collection
    #[cfg(feature = "ffi")]
    pub fn push_attr(&mut self, attr: OwnedAttribute) {
        self.headers.push(Header::Attribute(attr));
    }

    /// Add an all objects header (0x06) with the specified variation
    pub fn add_all_objects(self, variation: Variation) -> Self {
        self.add(ReadHeader::all_objects(variation).into())
    }

    /// Add 8-bit start/stop header (0x00) with the specified variation
    pub fn add_range_8(self, variation: Variation, start: u8, stop: u8) -> Self {
        self.add(ReadHeader::one_byte_range(variation, start, stop).into())
    }

    /// Add 16-bit start/stop header (0x01) with the specified variation
    pub fn add_range_16(self, variation: Variation, start: u16, stop: u16) -> Self {
        self.add(ReadHeader::two_byte_range(variation, start, stop).into())
    }

    /// add a one byte limited count header (0x7) with the specified count and variation
    pub fn add_one_byte_limited_count(self, variation: Variation, count: u8) -> Self {
        self.add(ReadHeader::one_byte_limited_count(variation, count).into())
    }

    /// add a two byte limited count (0x08) header with the specified count and variation
    pub fn add_two_byte_limited_count(self, variation: Variation, count: u16) -> Self {
        self.add(ReadHeader::two_byte_limited_count(variation, count).into())
    }

    /// Add a limited count (0x07) with a single g50v2
    ///
    /// This is useful when constructing freeze-at-requests
    pub fn add_time_and_interval(self, time: Timestamp, interval_ms: u32) -> Self {
        self.add_freeze_interval(FreezeInterval::new(time, interval_ms))
    }

    /// Add a limited count (0x07) with a single g50v2
    ///
    /// This is useful when constructing freeze-at-requests
    ///
    /// This can also be accomplished using the `add_time_and_interval` method although
    /// this method provides cleaner semantics.
    pub fn add_freeze_interval(self, interval: FreezeInterval) -> Self {
        self.add(Header::TimeAndInterval(interval))
    }

    /// Add an attribute header that will be encoded using 0x00 - one byte start/stop
    pub fn add_attribute(self, attr: OwnedAttribute) -> Self {
        self.add(Header::Attribute(attr))
    }

    pub(crate) fn write(&self, writer: &mut HeaderWriter) -> Result<(), TaskError> {
        for header in self.headers.iter() {
            header.format(writer)?;
        }
        Ok(())
    }

    fn add(mut self, header: Header) -> Self {
        self.headers.push(header);
        self
    }
}

/// Enum representing all of the READ request types available from the master API
#[derive(Clone, Debug)]
pub enum ReadRequest {
    /// Read a single header
    SingleHeader(ReadHeader),
    /// Read class data
    ClassScan(Classes),
    /// Read multiple headers
    MultipleHeader(Vec<ReadHeader>),
}

impl ReadRequest {
    /// construct a `ReadRequest` from a `Classes` instance
    pub fn class_scan(scan: Classes) -> Self {
        Self::ClassScan(scan)
    }

    /// construct a `ReadRequest` consisting of a single one-byte range
    pub fn one_byte_range(variation: Variation, start: u8, stop: u8) -> Self {
        Self::SingleHeader(ReadHeader::one_byte_range(variation, start, stop))
    }

    /// construct a `ReadRequest` consisting of a single one-byte range specifying a specific device attribute
    pub fn device_attribute<T: Into<u8>>(variation: T, set: AttrSet) -> Self {
        Self::one_byte_range(Group0(variation.into()), set.value(), set.value())
    }

    /// construct a `ReadRequest` consisting of a single two-byte range
    pub fn two_byte_range(variation: Variation, start: u16, stop: u16) -> Self {
        Self::SingleHeader(ReadHeader::two_byte_range(variation, start, stop))
    }

    /// construct an all objects `ReadRequest` for a particular variation
    pub fn all_objects(variation: Variation) -> Self {
        Self::SingleHeader(ReadHeader::all_objects(variation))
    }

    /// construct a `ReadRequest` consisting of multiple headers
    pub fn multiple_headers(headers: &[ReadHeader]) -> Self {
        Self::MultipleHeader(headers.to_vec())
    }

    pub(crate) fn format(&self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        match self {
            ReadRequest::SingleHeader(req) => req.format(writer),
            ReadRequest::ClassScan(req) => req.write(writer),
            ReadRequest::MultipleHeader(reqs) => {
                for req in reqs {
                    req.format(writer)?;
                }
                Ok(())
            }
        }
    }
}

impl From<ReadHeader> for Header {
    fn from(value: ReadHeader) -> Self {
        Header::Read(value)
    }
}

#[derive(Clone)]
pub(crate) enum CommandHeader {
    G12V1U8(Vec<(Group12Var1, u8)>),
    G41V1U8(Vec<(Group41Var1, u8)>),
    G41V2U8(Vec<(Group41Var2, u8)>),
    G41V3U8(Vec<(Group41Var3, u8)>),
    G41V4U8(Vec<(Group41Var4, u8)>),
    G12V1U16(Vec<(Group12Var1, u16)>),
    G41V1U16(Vec<(Group41Var1, u16)>),
    G41V2U16(Vec<(Group41Var2, u16)>),
    G41V3U16(Vec<(Group41Var3, u16)>),
    G41V4U16(Vec<(Group41Var4, u16)>),
}

pub(crate) trait Command {
    fn status(&self) -> CommandStatus;
    fn to_header_u8(&self, index: u8) -> CommandHeader;
    fn to_header_u16(&self, index: u16) -> CommandHeader;
}

impl Command for Group12Var1 {
    fn status(&self) -> CommandStatus {
        self.status
    }

    fn to_header_u8(&self, index: u8) -> CommandHeader {
        CommandHeader::G12V1U8(vec![(*self, index)])
    }

    fn to_header_u16(&self, index: u16) -> CommandHeader {
        CommandHeader::G12V1U16(vec![(*self, index)])
    }
}

impl Command for Group41Var1 {
    fn status(&self) -> CommandStatus {
        self.status
    }

    fn to_header_u8(&self, index: u8) -> CommandHeader {
        CommandHeader::G41V1U8(vec![(*self, index)])
    }

    fn to_header_u16(&self, index: u16) -> CommandHeader {
        CommandHeader::G41V1U16(vec![(*self, index)])
    }
}

impl Command for Group41Var2 {
    fn status(&self) -> CommandStatus {
        self.status
    }
    fn to_header_u8(&self, index: u8) -> CommandHeader {
        CommandHeader::G41V2U8(vec![(*self, index)])
    }
    fn to_header_u16(&self, index: u16) -> CommandHeader {
        CommandHeader::G41V2U16(vec![(*self, index)])
    }
}

impl Command for Group41Var3 {
    fn status(&self) -> CommandStatus {
        self.status
    }
    fn to_header_u8(&self, index: u8) -> CommandHeader {
        CommandHeader::G41V3U8(vec![(*self, index)])
    }
    fn to_header_u16(&self, index: u16) -> CommandHeader {
        CommandHeader::G41V3U16(vec![(*self, index)])
    }
}

impl Command for Group41Var4 {
    fn status(&self) -> CommandStatus {
        self.status
    }
    fn to_header_u8(&self, index: u8) -> CommandHeader {
        CommandHeader::G41V4U8(vec![(*self, index)])
    }

    fn to_header_u16(&self, index: u16) -> CommandHeader {
        CommandHeader::G41V4U16(vec![(*self, index)])
    }
}

/// Collection of command headers sent from the master API
pub struct CommandHeaders {
    headers: Vec<CommandHeader>,
}

impl CommandHeaders {
    pub(crate) fn single(header: CommandHeader) -> Self {
        Self {
            headers: vec![header],
        }
    }

    pub(crate) fn write(&self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        for header in self.headers.iter() {
            header.write(writer)?;
        }

        Ok(())
    }

    pub(crate) fn compare(&self, headers: HeaderCollection) -> Result<(), CommandResponseError> {
        let mut iter = headers.iter();

        for sent in &self.headers {
            match iter.next() {
                None => return Err(CommandResponseError::HeaderCountMismatch),
                Some(received) => sent.compare(received.details)?,
            }
        }

        if iter.next().is_some() {
            return Err(CommandResponseError::HeaderCountMismatch);
        }

        Ok(())
    }
}

/// Builder object used to create a [CommandHeaders](CommandHeaders)
#[derive(Clone)]
pub struct CommandBuilder {
    headers: Vec<CommandHeader>,
    partial: Option<CommandHeader>,
}

/// Trait that provides builder support for a particular command type
pub trait CommandSupport<T> {
    /// add a command using one byte addressing
    fn add_u8(&mut self, command: T, index: u8);
    /// add a command using two byte addressing
    fn add_u16(&mut self, command: T, index: u16);
    /// construct a `CommandHeaders` instance consisting of single command with one byte addressing
    fn single_header_u8(command: T, index: u8) -> CommandHeaders;
    /// construct a `CommandHeaders` instance consisting of single command with two byte addressing
    fn single_header_u16(command: T, index: u16) -> CommandHeaders;
}

impl CommandBuilder {
    /// construct a new `CommandBuilder` instance
    pub fn new() -> Self {
        Self {
            headers: Vec::new(),
            partial: None,
        }
    }

    /// Manually complete any partially built header.
    ///
    /// This allows for building multiple headers of the same type,
    /// e.g. two g12v1 values in two separate headers
    pub fn finish_header(&mut self) {
        if let Some(header) = self.partial.take() {
            self.headers.push(header);
        }
    }

    fn add_g12v1_u8(&mut self, command: Group12Var1, index: u8) {
        if let Some(partial) = self.partial.take() {
            if let CommandHeader::G12V1U8(mut vec) = partial {
                vec.push((command, index));
                self.partial = Some(CommandHeader::G12V1U8(vec));
            } else {
                self.headers.push(partial);
                self.partial = Some(command.to_header_u8(index));
            }
        } else {
            self.partial = Some(command.to_header_u8(index));
        }
    }

    fn add_g12v1_u16(&mut self, command: Group12Var1, index: u16) {
        if let Some(partial) = self.partial.take() {
            if let CommandHeader::G12V1U16(mut vec) = partial {
                vec.push((command, index));
                self.partial = Some(CommandHeader::G12V1U16(vec));
            } else {
                self.headers.push(partial);
                self.partial = Some(command.to_header_u16(index));
            }
        } else {
            self.partial = Some(command.to_header_u16(index));
        }
    }

    fn add_g41v1_u8(&mut self, command: Group41Var1, index: u8) {
        if let Some(partial) = self.partial.take() {
            if let CommandHeader::G41V1U8(mut vec) = partial {
                vec.push((command, index));
                self.partial = Some(CommandHeader::G41V1U8(vec));
            } else {
                self.headers.push(partial);
                self.partial = Some(command.to_header_u8(index));
            }
        } else {
            self.partial = Some(command.to_header_u8(index));
        }
    }

    fn add_g41v1_u16(&mut self, command: Group41Var1, index: u16) {
        if let Some(partial) = self.partial.take() {
            if let CommandHeader::G41V1U16(mut vec) = partial {
                vec.push((command, index));
                self.partial = Some(CommandHeader::G41V1U16(vec));
            } else {
                self.headers.push(partial);
                self.partial = Some(command.to_header_u16(index));
            }
        } else {
            self.partial = Some(command.to_header_u16(index));
        }
    }

    fn add_g41v2_u8(&mut self, command: Group41Var2, index: u8) {
        if let Some(partial) = self.partial.take() {
            if let CommandHeader::G41V2U8(mut vec) = partial {
                vec.push((command, index));
                self.partial = Some(CommandHeader::G41V2U8(vec));
            } else {
                self.headers.push(partial);
                self.partial = Some(command.to_header_u8(index));
            }
        } else {
            self.partial = Some(command.to_header_u8(index));
        }
    }

    fn add_g41v2_u16(&mut self, command: Group41Var2, index: u16) {
        if let Some(partial) = self.partial.take() {
            if let CommandHeader::G41V2U16(mut vec) = partial {
                vec.push((command, index));
                self.partial = Some(CommandHeader::G41V2U16(vec));
            } else {
                self.headers.push(partial);
                self.partial = Some(command.to_header_u16(index));
            }
        } else {
            self.partial = Some(command.to_header_u16(index));
        }
    }

    fn add_g41v3_u8(&mut self, command: Group41Var3, index: u8) {
        if let Some(partial) = self.partial.take() {
            if let CommandHeader::G41V3U8(mut vec) = partial {
                vec.push((command, index));
                self.partial = Some(CommandHeader::G41V3U8(vec));
            } else {
                self.headers.push(partial);
                self.partial = Some(command.to_header_u8(index));
            }
        } else {
            self.partial = Some(command.to_header_u8(index));
        }
    }

    fn add_g41v3_u16(&mut self, command: Group41Var3, index: u16) {
        if let Some(partial) = self.partial.take() {
            if let CommandHeader::G41V3U16(mut vec) = partial {
                vec.push((command, index));
                self.partial = Some(CommandHeader::G41V3U16(vec));
            } else {
                self.headers.push(partial);
                self.partial = Some(command.to_header_u16(index));
            }
        } else {
            self.partial = Some(command.to_header_u16(index));
        }
    }

    fn add_g41v4_u8(&mut self, command: Group41Var4, index: u8) {
        if let Some(partial) = self.partial.take() {
            if let CommandHeader::G41V4U8(mut vec) = partial {
                vec.push((command, index));
                self.partial = Some(CommandHeader::G41V4U8(vec));
            } else {
                self.headers.push(partial);
                self.partial = Some(command.to_header_u8(index));
            }
        } else {
            self.partial = Some(command.to_header_u8(index));
        }
    }

    fn add_g41v4_u16(&mut self, command: Group41Var4, index: u16) {
        if let Some(partial) = self.partial.take() {
            if let CommandHeader::G41V4U16(mut vec) = partial {
                vec.push((command, index));
                self.partial = Some(CommandHeader::G41V4U16(vec));
            } else {
                self.headers.push(partial);
                self.partial = Some(command.to_header_u16(index));
            }
        } else {
            self.partial = Some(command.to_header_u16(index));
        }
    }

    /// Consume the instance and return a fully built `CommandHeaders`
    pub fn build(mut self) -> CommandHeaders {
        self.finish_header();
        CommandHeaders {
            headers: self.headers,
        }
    }
}

impl CommandSupport<Group12Var1> for CommandBuilder {
    fn add_u8(&mut self, command: Group12Var1, index: u8) {
        self.add_g12v1_u8(command, index);
    }

    fn add_u16(&mut self, command: Group12Var1, index: u16) {
        self.add_g12v1_u16(command, index);
    }

    fn single_header_u8(command: Group12Var1, index: u8) -> CommandHeaders {
        CommandHeaders::single(command.to_header_u8(index))
    }

    fn single_header_u16(command: Group12Var1, index: u16) -> CommandHeaders {
        CommandHeaders::single(command.to_header_u16(index))
    }
}

impl CommandSupport<Group41Var1> for CommandBuilder {
    fn add_u8(&mut self, command: Group41Var1, index: u8) {
        self.add_g41v1_u8(command, index);
    }

    fn add_u16(&mut self, command: Group41Var1, index: u16) {
        self.add_g41v1_u16(command, index);
    }

    fn single_header_u8(command: Group41Var1, index: u8) -> CommandHeaders {
        CommandHeaders::single(command.to_header_u8(index))
    }

    fn single_header_u16(command: Group41Var1, index: u16) -> CommandHeaders {
        CommandHeaders::single(command.to_header_u16(index))
    }
}

impl CommandSupport<Group41Var2> for CommandBuilder {
    fn add_u8(&mut self, command: Group41Var2, index: u8) {
        self.add_g41v2_u8(command, index);
    }

    fn add_u16(&mut self, command: Group41Var2, index: u16) {
        self.add_g41v2_u16(command, index);
    }

    fn single_header_u8(command: Group41Var2, index: u8) -> CommandHeaders {
        CommandHeaders::single(command.to_header_u8(index))
    }

    fn single_header_u16(command: Group41Var2, index: u16) -> CommandHeaders {
        CommandHeaders::single(command.to_header_u16(index))
    }
}

impl CommandSupport<Group41Var3> for CommandBuilder {
    fn add_u8(&mut self, command: Group41Var3, index: u8) {
        self.add_g41v3_u8(command, index);
    }

    fn add_u16(&mut self, command: Group41Var3, index: u16) {
        self.add_g41v3_u16(command, index);
    }

    fn single_header_u8(command: Group41Var3, index: u8) -> CommandHeaders {
        CommandHeaders::single(command.to_header_u8(index))
    }

    fn single_header_u16(command: Group41Var3, index: u16) -> CommandHeaders {
        CommandHeaders::single(command.to_header_u16(index))
    }
}

impl CommandSupport<Group41Var4> for CommandBuilder {
    fn add_u8(&mut self, command: Group41Var4, index: u8) {
        self.add_g41v4_u8(command, index);
    }

    fn add_u16(&mut self, command: Group41Var4, index: u16) {
        self.add_g41v4_u16(command, index);
    }

    fn single_header_u8(command: Group41Var4, index: u8) -> CommandHeaders {
        CommandHeaders::single(command.to_header_u8(index))
    }

    fn single_header_u16(command: Group41Var4, index: u16) -> CommandHeaders {
        CommandHeaders::single(command.to_header_u16(index))
    }
}

impl Default for CommandBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl CommandHeader {
    pub(crate) fn write(&self, writer: &mut HeaderWriter) -> Result<(), scursor::WriteError> {
        match self {
            CommandHeader::G12V1U8(items) => writer.write_prefixed_items(items.iter()),
            CommandHeader::G41V1U8(items) => writer.write_prefixed_items(items.iter()),
            CommandHeader::G41V2U8(items) => writer.write_prefixed_items(items.iter()),
            CommandHeader::G41V3U8(items) => writer.write_prefixed_items(items.iter()),
            CommandHeader::G41V4U8(items) => writer.write_prefixed_items(items.iter()),
            CommandHeader::G12V1U16(items) => writer.write_prefixed_items(items.iter()),
            CommandHeader::G41V1U16(items) => writer.write_prefixed_items(items.iter()),
            CommandHeader::G41V2U16(items) => writer.write_prefixed_items(items.iter()),
            CommandHeader::G41V3U16(items) => writer.write_prefixed_items(items.iter()),
            CommandHeader::G41V4U16(items) => writer.write_prefixed_items(items.iter()),
        }
    }

    fn compare_items<V, I>(
        seq: CountSequence<'_, Prefix<I, V>>,
        sent: &[(V, I)],
    ) -> Result<(), CommandResponseError>
    where
        V: FixedSizeVariation + Command,
        I: Index,
    {
        let mut received = seq.iter();

        for item in sent {
            match received.next() {
                None => return Err(CommandResponseError::ObjectCountMismatch),
                Some(x) => {
                    if x.value.status() != CommandStatus::Success {
                        return Err(CommandResponseError::BadStatus(x.value.status()));
                    }
                    if !x.equals(item) {
                        return Err(CommandResponseError::ObjectValueMismatch);
                    }
                }
            }
        }

        if received.next().is_some() {
            return Err(CommandResponseError::ObjectCountMismatch);
        }

        Ok(())
    }

    pub(crate) fn compare(&self, response: HeaderDetails) -> Result<(), CommandResponseError> {
        match self {
            CommandHeader::G12V1U8(items) => match response {
                HeaderDetails::OneByteCountAndPrefix(_, PrefixedVariation::Group12Var1(seq)) => {
                    Self::compare_items(seq, items)
                }
                _ => Err(CommandResponseError::HeaderTypeMismatch),
            },
            CommandHeader::G12V1U16(items) => match response {
                HeaderDetails::TwoByteCountAndPrefix(_, PrefixedVariation::Group12Var1(seq)) => {
                    Self::compare_items(seq, items)
                }
                _ => Err(CommandResponseError::HeaderTypeMismatch),
            },
            CommandHeader::G41V1U8(items) => match response {
                HeaderDetails::OneByteCountAndPrefix(_, PrefixedVariation::Group41Var1(seq)) => {
                    Self::compare_items(seq, items)
                }
                _ => Err(CommandResponseError::HeaderTypeMismatch),
            },
            CommandHeader::G41V1U16(items) => match response {
                HeaderDetails::TwoByteCountAndPrefix(_, PrefixedVariation::Group41Var1(seq)) => {
                    Self::compare_items(seq, items)
                }
                _ => Err(CommandResponseError::HeaderTypeMismatch),
            },
            CommandHeader::G41V2U8(items) => match response {
                HeaderDetails::OneByteCountAndPrefix(_, PrefixedVariation::Group41Var2(seq)) => {
                    Self::compare_items(seq, items)
                }
                _ => Err(CommandResponseError::HeaderTypeMismatch),
            },
            CommandHeader::G41V2U16(items) => match response {
                HeaderDetails::TwoByteCountAndPrefix(_, PrefixedVariation::Group41Var2(seq)) => {
                    Self::compare_items(seq, items)
                }
                _ => Err(CommandResponseError::HeaderTypeMismatch),
            },
            CommandHeader::G41V3U8(items) => match response {
                HeaderDetails::OneByteCountAndPrefix(_, PrefixedVariation::Group41Var3(seq)) => {
                    Self::compare_items(seq, items)
                }
                _ => Err(CommandResponseError::HeaderTypeMismatch),
            },
            CommandHeader::G41V3U16(items) => match response {
                HeaderDetails::TwoByteCountAndPrefix(_, PrefixedVariation::Group41Var3(seq)) => {
                    Self::compare_items(seq, items)
                }
                _ => Err(CommandResponseError::HeaderTypeMismatch),
            },
            CommandHeader::G41V4U8(items) => match response {
                HeaderDetails::OneByteCountAndPrefix(_, PrefixedVariation::Group41Var4(seq)) => {
                    Self::compare_items(seq, items)
                }
                _ => Err(CommandResponseError::HeaderTypeMismatch),
            },
            CommandHeader::G41V4U16(items) => match response {
                HeaderDetails::TwoByteCountAndPrefix(_, PrefixedVariation::Group41Var4(seq)) => {
                    Self::compare_items(seq, items)
                }
                _ => Err(CommandResponseError::HeaderTypeMismatch),
            },
        }
    }
}