matter-kit 0.3.0

Matter (CSA smart-home standard) protocol implementation in Rust: no_std, no-alloc, executor-agnostic
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
// Generated by `cargo xtask` from the CSA data model 1.6. Do not edit.
//
// Camera AV Settings User Level Management — cluster 0x0552.
//
// Regenerate with `cargo xtask clusters --dm <checkout>/data_model/1.6`; `cargo xtask check`
// is the CI gate that keeps this file honest. The XML itself is not vendored here — it
// carries the CSA's own copyright notice and is not redistributable.

//! Camera AV Settings User Level Management (cluster `0x0552`, revision 1).
//!
//! 17 identifiers, 5 feature bits, and the specification's own conformance for
//! every element. `CLUSTER` is the whole table.

#![allow(unused_imports)]

use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
use crate::dm::meta::{EventPriority, Reporting};
use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
use crate::error::Result;
use crate::im::{AttributeId, ClusterId, CommandId, EventId};
use crate::tlv::{
    ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
};

/// The cluster id.
pub const ID: ClusterId = 0x0552;

/// The highest revision the specification's history defines — what `ClusterRevision`
/// must report (Core §7.13.1).
pub const REVISION: u16 = 1;

/// The PICS code, which a certification declaration names the cluster by.
pub const PICS: &str = "AVSUM";

/// The `FeatureMap` bits (Core §7.13.2).
///
/// A feature has conformance of its own: a device may not claim one the
/// specification forbids it alongside another.
pub mod feature {
    /// Digital PTZ support
    ///
    /// Code `DPTZ`, bit 0.
    pub const DIGITAL_PTZ: u32 = 1 << 0;
    /// Mechanical Pan support
    ///
    /// Code `MPAN`, bit 1.
    pub const MECHANICAL_PAN: u32 = 1 << 1;
    /// Mechanical Tilt support
    ///
    /// Code `MTILT`, bit 2.
    pub const MECHANICAL_TILT: u32 = 1 << 2;
    /// Mechanical Zoom support
    ///
    /// Code `MZOOM`, bit 3.
    pub const MECHANICAL_ZOOM: u32 = 1 << 3;
    /// Mechanical saved presets support
    ///
    /// Code `MPRESETS`, bit 4.
    pub const MECHANICAL_PRESETS: u32 = 1 << 4;
}

/// Every feature bit, with its conformance.
pub const FEATURES: &[Feature] = &[
    Feature {
        bit: 0,
        code: "DPTZ",
        name: "DigitalPTZ",
        conform: Conform::OPTIONAL,
    },
    Feature {
        bit: 1,
        code: "MPAN",
        name: "MechanicalPan",
        conform: Conform::OPTIONAL,
    },
    Feature {
        bit: 2,
        code: "MTILT",
        name: "MechanicalTilt",
        conform: Conform::OPTIONAL,
    },
    Feature {
        bit: 3,
        code: "MZOOM",
        name: "MechanicalZoom",
        conform: Conform::OPTIONAL,
    },
    Feature {
        bit: 4,
        code: "MPRESETS",
        name: "MechanicalPresets",
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Optional,
                when: Condition::Any(&[
                    Condition::Feature(1),
                    Condition::Feature(2),
                    Condition::Feature(3),
                ]),
            }],
        },
    },
];

/// The attribute ids.
pub mod attribute {
    use crate::im::AttributeId;

    /// `MPTZPosition`.
    pub const MPTZ_POSITION: AttributeId = 0x00;
    /// `MaxPresets`.
    pub const MAX_PRESETS: AttributeId = 0x01;
    /// `MPTZPresets`.
    pub const MPTZ_PRESETS: AttributeId = 0x02;
    /// `DPTZStreams`.
    pub const DPTZ_STREAMS: AttributeId = 0x03;
    /// `ZoomMax`.
    pub const ZOOM_MAX: AttributeId = 0x04;
    /// `TiltMin`.
    pub const TILT_MIN: AttributeId = 0x05;
    /// `TiltMax`.
    pub const TILT_MAX: AttributeId = 0x06;
    /// `PanMin`.
    pub const PAN_MIN: AttributeId = 0x07;
    /// `PanMax`.
    pub const PAN_MAX: AttributeId = 0x08;
    /// `MovementState`.
    pub const MOVEMENT_STATE: AttributeId = 0x09;
}

/// The command ids.
pub mod command {
    use crate::im::CommandId;

    /// `MPTZSetPosition`.
    pub const MPTZ_SET_POSITION: CommandId = 0x00;
    /// `MPTZRelativeMove`.
    pub const MPTZ_RELATIVE_MOVE: CommandId = 0x01;
    /// `MPTZMoveToPreset`.
    pub const MPTZ_MOVE_TO_PRESET: CommandId = 0x02;
    /// `MPTZSavePreset`.
    pub const MPTZ_SAVE_PRESET: CommandId = 0x03;
    /// `MPTZRemovePreset`.
    pub const MPTZ_REMOVE_PRESET: CommandId = 0x04;
    /// `DPTZSetViewport`.
    pub const DPTZ_SET_VIEWPORT: CommandId = 0x05;
    /// `DPTZRelativeMove`.
    pub const DPTZ_RELATIVE_MOVE: CommandId = 0x06;
}

/// `PhysicalMovementEnum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum PhysicalMovementEnum {
    /// The camera is idle.
    Idle = 0,
    /// The camera is moving to a new value of Pan, Tilt, and/or Zoom.
    Moving = 1,
}

impl PhysicalMovementEnum {
    /// Reads a wire value, or `None` for one this revision does not define.
    #[must_use]
    pub const fn from_value(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::Idle),
            1 => Some(Self::Moving),
            _ => None,
        }
    }

    /// Its wire value.
    #[must_use]
    pub const fn value(self) -> u8 {
        self as u8
    }
}

impl ToTlv for PhysicalMovementEnum {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        self.value().to_tlv(w, tag)
    }
}

impl<'a> FromTlv<'a> for PhysicalMovementEnum {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
        Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
    }
}

/// `DPTZStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DPTZStruct {
    /// `VideoStreamID`, field 0.
    pub video_stream_id: u16,
    /// `Viewport`, field 1.
    pub viewport: crate::clusters::generated::globals::ViewportStruct,
}

impl ToTlv for DPTZStruct {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.video_stream_id.to_tlv(w, Tag::Context(0))?;
        self.viewport.to_tlv(w, Tag::Context(1))?;
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for DPTZStruct {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut video_stream_id = None;
        let mut viewport = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => video_stream_id = Some(<u16>::from_tlv(reader, &field)?),
                Some(1) => {
                    viewport = Some(
                        <crate::clusters::generated::globals::ViewportStruct>::from_tlv(
                            reader, &field,
                        )?,
                    )
                }
                // A field this revision does not define. §7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self {
            video_stream_id: video_stream_id
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            viewport: viewport.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// `MPTZPresetStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MPTZPresetStruct<'a> {
    /// `PresetID`, field 0.
    pub preset_id: u8,
    /// `Name`, field 1.
    pub name: &'a str,
    /// `Settings`, field 2.
    pub settings: MPTZStruct,
}

impl<'a> ToTlv for MPTZPresetStruct<'a> {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.preset_id.to_tlv(w, Tag::Context(0))?;
        self.name.to_tlv(w, Tag::Context(1))?;
        self.settings.to_tlv(w, Tag::Context(2))?;
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for MPTZPresetStruct<'a> {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut preset_id = None;
        let mut name = None;
        let mut settings = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => preset_id = Some(<u8>::from_tlv(reader, &field)?),
                Some(1) => name = Some(<&'a str>::from_tlv(reader, &field)?),
                Some(2) => settings = Some(<MPTZStruct>::from_tlv(reader, &field)?),
                // A field this revision does not define. §7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self {
            preset_id: preset_id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            name: name.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            settings: settings.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// `MPTZStruct`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MPTZStruct {
    /// `Pan`, field 0.
    pub pan: Option<i16>,
    /// `Tilt`, field 1.
    pub tilt: Option<i16>,
    /// `Zoom`, field 2.
    pub zoom: Option<u8>,
}

impl ToTlv for MPTZStruct {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        if let Some(value) = &self.pan {
            value.to_tlv(w, Tag::Context(0))?;
        }
        if let Some(value) = &self.tilt {
            value.to_tlv(w, Tag::Context(1))?;
        }
        if let Some(value) = &self.zoom {
            value.to_tlv(w, Tag::Context(2))?;
        }
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for MPTZStruct {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut pan = None;
        let mut tilt = None;
        let mut zoom = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => pan = Some(<i16>::from_tlv(reader, &field)?),
                Some(1) => tilt = Some(<i16>::from_tlv(reader, &field)?),
                Some(2) => zoom = Some(<u8>::from_tlv(reader, &field)?),
                // A field this revision does not define. §7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self { pan, tilt, zoom })
    }
}

/// The fields of `MPTZSetPosition` (command `0x00`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MPTZSetPositionFields {
    /// `Pan`, field 0.
    pub pan: Option<i16>,
    /// `Tilt`, field 1.
    pub tilt: Option<i16>,
    /// `Zoom`, field 2.
    pub zoom: Option<u8>,
}

impl ToTlv for MPTZSetPositionFields {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        if let Some(value) = &self.pan {
            value.to_tlv(w, Tag::Context(0))?;
        }
        if let Some(value) = &self.tilt {
            value.to_tlv(w, Tag::Context(1))?;
        }
        if let Some(value) = &self.zoom {
            value.to_tlv(w, Tag::Context(2))?;
        }
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for MPTZSetPositionFields {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut pan = None;
        let mut tilt = None;
        let mut zoom = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => pan = Some(<i16>::from_tlv(reader, &field)?),
                Some(1) => tilt = Some(<i16>::from_tlv(reader, &field)?),
                Some(2) => zoom = Some(<u8>::from_tlv(reader, &field)?),
                // A field this revision does not define. §7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self { pan, tilt, zoom })
    }
}

/// The fields of `MPTZRelativeMove` (command `0x01`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MPTZRelativeMoveFields {
    /// `PanDelta`, field 0.
    pub pan_delta: Option<i16>,
    /// `TiltDelta`, field 1.
    pub tilt_delta: Option<i16>,
    /// `ZoomDelta`, field 2.
    pub zoom_delta: Option<i8>,
}

impl ToTlv for MPTZRelativeMoveFields {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        if let Some(value) = &self.pan_delta {
            value.to_tlv(w, Tag::Context(0))?;
        }
        if let Some(value) = &self.tilt_delta {
            value.to_tlv(w, Tag::Context(1))?;
        }
        if let Some(value) = &self.zoom_delta {
            value.to_tlv(w, Tag::Context(2))?;
        }
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for MPTZRelativeMoveFields {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut pan_delta = None;
        let mut tilt_delta = None;
        let mut zoom_delta = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => pan_delta = Some(<i16>::from_tlv(reader, &field)?),
                Some(1) => tilt_delta = Some(<i16>::from_tlv(reader, &field)?),
                Some(2) => zoom_delta = Some(<i8>::from_tlv(reader, &field)?),
                // A field this revision does not define. §7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self {
            pan_delta,
            tilt_delta,
            zoom_delta,
        })
    }
}

/// The fields of `MPTZMoveToPreset` (command `0x02`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MPTZMoveToPresetFields {
    /// `PresetID`, field 0.
    pub preset_id: u8,
}

impl ToTlv for MPTZMoveToPresetFields {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.preset_id.to_tlv(w, Tag::Context(0))?;
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for MPTZMoveToPresetFields {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut preset_id = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => preset_id = Some(<u8>::from_tlv(reader, &field)?),
                // A field this revision does not define. §7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self {
            preset_id: preset_id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// The fields of `MPTZSavePreset` (command `0x03`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MPTZSavePresetFields<'a> {
    /// `PresetID`, field 0.
    pub preset_id: Option<u8>,
    /// `Name`, field 1.
    pub name: &'a str,
}

impl<'a> ToTlv for MPTZSavePresetFields<'a> {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        if let Some(value) = &self.preset_id {
            value.to_tlv(w, Tag::Context(0))?;
        }
        self.name.to_tlv(w, Tag::Context(1))?;
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for MPTZSavePresetFields<'a> {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut preset_id = None;
        let mut name = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => preset_id = Some(<u8>::from_tlv(reader, &field)?),
                Some(1) => name = Some(<&'a str>::from_tlv(reader, &field)?),
                // A field this revision does not define. §7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self {
            preset_id,
            name: name.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// The fields of `MPTZRemovePreset` (command `0x04`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MPTZRemovePresetFields {
    /// `PresetID`, field 0.
    pub preset_id: u8,
}

impl ToTlv for MPTZRemovePresetFields {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.preset_id.to_tlv(w, Tag::Context(0))?;
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for MPTZRemovePresetFields {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut preset_id = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => preset_id = Some(<u8>::from_tlv(reader, &field)?),
                // A field this revision does not define. §7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self {
            preset_id: preset_id.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// The fields of `DPTZSetViewport` (command `0x05`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DPTZSetViewportFields {
    /// `VideoStreamID`, field 0.
    pub video_stream_id: u16,
    /// `Viewport`, field 1.
    pub viewport: crate::clusters::generated::globals::ViewportStruct,
}

impl ToTlv for DPTZSetViewportFields {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.video_stream_id.to_tlv(w, Tag::Context(0))?;
        self.viewport.to_tlv(w, Tag::Context(1))?;
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for DPTZSetViewportFields {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut video_stream_id = None;
        let mut viewport = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => video_stream_id = Some(<u16>::from_tlv(reader, &field)?),
                Some(1) => {
                    viewport = Some(
                        <crate::clusters::generated::globals::ViewportStruct>::from_tlv(
                            reader, &field,
                        )?,
                    )
                }
                // A field this revision does not define. §7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self {
            video_stream_id: video_stream_id
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            viewport: viewport.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
        })
    }
}

/// The fields of `DPTZRelativeMove` (command `0x06`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DPTZRelativeMoveFields {
    /// `VideoStreamID`, field 0.
    pub video_stream_id: u16,
    /// `DeltaX`, field 1.
    pub delta_x: Option<i16>,
    /// `DeltaY`, field 2.
    pub delta_y: Option<i16>,
    /// `ZoomDelta`, field 3.
    pub zoom_delta: Option<i8>,
}

impl ToTlv for DPTZRelativeMoveFields {
    fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
        w.start_structure(tag)?;
        self.video_stream_id.to_tlv(w, Tag::Context(0))?;
        if let Some(value) = &self.delta_x {
            value.to_tlv(w, Tag::Context(1))?;
        }
        if let Some(value) = &self.delta_y {
            value.to_tlv(w, Tag::Context(2))?;
        }
        if let Some(value) = &self.zoom_delta {
            value.to_tlv(w, Tag::Context(3))?;
        }
        w.end_container()
    }
}

impl<'a> FromTlv<'a> for DPTZRelativeMoveFields {
    fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
        if element.value.container() != Some(ContainerKind::Structure) {
            return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
        }
        let mut video_stream_id = None;
        let mut delta_x = None;
        let mut delta_y = None;
        let mut zoom_delta = None;
        loop {
            let Some(field) = reader.next_element()? else {
                return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
            };
            if field.value == Value::EndOfContainer {
                break;
            }
            match field.tag.context() {
                Some(0) => video_stream_id = Some(<u16>::from_tlv(reader, &field)?),
                Some(1) => delta_x = Some(<i16>::from_tlv(reader, &field)?),
                Some(2) => delta_y = Some(<i16>::from_tlv(reader, &field)?),
                Some(3) => zoom_delta = Some(<i8>::from_tlv(reader, &field)?),
                // A field this revision does not define. §7.19.2: a receiver
                // ignores what it does not know rather than refusing the whole
                // structure, which is what lets a newer peer talk to an older one.
                _ => reader.skip_value(&field)?,
            }
        }
        Ok(Self {
            video_stream_id: video_stream_id
                .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
            delta_x,
            delta_y,
            zoom_delta,
        })
    }
}

/// Every attribute the specification defines, with its conformance.
pub const ATTRIBUTES: &[Attribute] = &[
    Attribute {
        id: 0x0000,
        name: "MPTZPosition",
        kind: "MPTZStruct",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::NON_VOLATILE,
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Any(&[
                    Condition::Feature(1),
                    Condition::Feature(2),
                    Condition::Feature(3),
                ]),
            }],
        },
    },
    Attribute {
        id: 0x0001,
        name: "MaxPresets",
        kind: "uint8",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::FIXED,
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(4),
            }],
        },
    },
    Attribute {
        id: 0x0002,
        name: "MPTZPresets",
        kind: "list[MPTZPresetStruct]",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::NON_VOLATILE,
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(4),
            }],
        },
    },
    Attribute {
        id: 0x0003,
        name: "DPTZStreams",
        kind: "list[DPTZStruct]",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::NON_VOLATILE,
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(0),
            }],
        },
    },
    Attribute {
        id: 0x0004,
        name: "ZoomMax",
        kind: "uint8",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(3),
            }],
        },
    },
    Attribute {
        id: 0x0005,
        name: "TiltMin",
        kind: "int16",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(2),
            }],
        },
    },
    Attribute {
        id: 0x0006,
        name: "TiltMax",
        kind: "int16",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(2),
            }],
        },
    },
    Attribute {
        id: 0x0007,
        name: "PanMin",
        kind: "int16",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(1),
            }],
        },
    },
    Attribute {
        id: 0x0008,
        name: "PanMax",
        kind: "int16",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(1),
            }],
        },
    },
    Attribute {
        id: 0x0009,
        name: "MovementState",
        kind: "PhysicalMovementEnum",
        access: Access::read_only(Privilege::View),
        qualities: AttributeQualities::empty(),
        reporting: Reporting::OnChange,
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Any(&[
                    Condition::Feature(1),
                    Condition::Feature(2),
                    Condition::Feature(3),
                ]),
            }],
        },
    },
];

/// Every command, with its conformance.
pub const COMMANDS: &[Command] = &[
    Command {
        id: 0x00,
        name: "MPTZSetPosition",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Any(&[
                    Condition::Feature(1),
                    Condition::Feature(2),
                    Condition::Feature(3),
                ]),
            }],
        },
    },
    Command {
        id: 0x01,
        name: "MPTZRelativeMove",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Any(&[
                    Condition::Feature(1),
                    Condition::Feature(2),
                    Condition::Feature(3),
                ]),
            }],
        },
    },
    Command {
        id: 0x02,
        name: "MPTZMoveToPreset",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(4),
            }],
        },
    },
    Command {
        id: 0x03,
        name: "MPTZSavePreset",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(4),
            }],
        },
    },
    Command {
        id: 0x04,
        name: "MPTZRemovePreset",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(4),
            }],
        },
    },
    Command {
        id: 0x05,
        name: "DPTZSetViewport",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Mandatory,
                when: Condition::Feature(0),
            }],
        },
    },
    Command {
        id: 0x06,
        name: "DPTZRelativeMove",
        to_server: true,
        response: None,
        access: Access::invoke(Privilege::Operate),
        conform: Conform {
            clauses: &[Clause {
                verdict: Conformance::Optional,
                when: Condition::Feature(0),
            }],
        },
    },
];

/// Every event, with its conformance.
pub const EVENTS: &[Event] = &[];

/// The whole cluster, as the specification defines it.
pub const CLUSTER: Cluster = Cluster {
    id: ID,
    name: "Camera AV Settings User Level Management",
    revision: REVISION,
    pics: "AVSUM",
    derived_from: None,
    features: FEATURES,
    attributes: ATTRIBUTES,
    commands: COMMANDS,
    events: EVENTS,
};