mx-remote 9.1.0

Client library for Pulse-Eight MatrixOS devices over UDP multicast/broadcast
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
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
// Author: Lars Op den Kamp (lars@opdenkamp-it.nl)
// Copyright (c) 2026 Op den Kamp IT Solutions

//! Wire enumerations and bitmasks.
//!
//! Each type is a newtype over the integer that travels on the wire, with
//! named constants rather than a closed set of variants. A value this library
//! has no name for reaches the caller as it arrived: zero is a valid value for
//! most of these, so a confidently wrong reading is worse than an unrecognised
//! one.

use core::fmt;
use core::ops::{BitAnd, BitOr, BitOrAssign};

/// Declares a bitmask newtype with the given named bit constants.
///
/// The representation defaults to `u32`; give it explicitly as `Name: u64` for
/// a mask whose wire field is wider.
macro_rules! bitmask {
    (
        $(#[$meta:meta])*
        $name:ident { $( $(#[$cmeta:meta])* $cname:ident = $value:expr; )* }
    ) => {
        bitmask! {
            $(#[$meta])*
            $name: u32 { $( $(#[$cmeta])* $cname = $value; )* }
        }
    };
    (
        $(#[$meta:meta])*
        $name:ident: $repr:ty { $( $(#[$cmeta:meta])* $cname:ident = $value:expr; )* }
    ) => {
        $(#[$meta])*
        #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct $name($repr);

        impl $name {
            /// No bits set.
            pub const NONE: Self = Self(0);

            $( $(#[$cmeta])* pub const $cname: Self = Self($value); )*

            /// Wraps a raw wire value, including bits this library has no name for.
            pub const fn from_bits(bits: $repr) -> Self {
                Self(bits)
            }

            /// Returns the raw wire value.
            pub const fn bits(self) -> $repr {
                self.0
            }

            /// Reports whether every bit in `other` is set.
            pub const fn has(self, other: Self) -> bool {
                self.0 & other.0 == other.0
            }

            /// Reports whether no bit is set.
            pub const fn is_empty(self) -> bool {
                self.0 == 0
            }
        }

        impl BitOr for $name {
            type Output = Self;
            fn bitor(self, rhs: Self) -> Self {
                Self(self.0 | rhs.0)
            }
        }

        impl BitOrAssign for $name {
            fn bitor_assign(&mut self, rhs: Self) {
                self.0 |= rhs.0;
            }
        }

        impl BitAnd for $name {
            type Output = Self;
            fn bitand(self, rhs: Self) -> Self {
                Self(self.0 & rhs.0)
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                // Two hex digits per byte of the wire field, plus "0x".
                write!(
                    f,
                    "{:#0width$x}",
                    self.0,
                    width = core::mem::size_of::<$repr>() * 2 + 2
                )
            }
        }
    };
}

/// Declares an enumeration newtype over `$repr` with the given named constants.
macro_rules! wire_enum {
    (
        $(#[$meta:meta])*
        $name:ident: $repr:ty { $( $(#[$cmeta:meta])* $cname:ident = $value:expr; )* }
    ) => {
        $(#[$meta])*
        #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct $name($repr);

        impl $name {
            $( $(#[$cmeta])* pub const $cname: Self = Self($value); )*

            /// Wraps a raw wire value, including one this library has no name for.
            pub const fn from_wire(value: $repr) -> Self {
                Self(value)
            }

            /// Returns the raw wire value.
            pub const fn to_wire(self) -> $repr {
                self.0
            }
        }
    };
}

bitmask! {
    /// Capabilities a device reports in its hello frame.
    DeviceFeature {
        /// Receives infrared.
        IR_RX = 1 << 0;
        /// Transmits infrared.
        IR_TX = 1 << 1;
        /// Speaks CEC.
        CEC = 1 << 2;
        /// Acts as a OneIP stream source.
        V2IP_SOURCE = 1 << 3;
        /// Acts as a OneIP stream sink.
        V2IP_SINK = 1 << 4;
        /// Routes video.
        VIDEO_ROUTING = 1 << 5;
        /// Routes audio.
        AUDIO_ROUTING = 1 << 6;
        /// Controls volume.
        VOLUME_CONTROL = 1 << 7;
        /// Supports audio return.
        AUDIO_RETURN = 1 << 8;
        /// Passes remote-control commands through.
        REMOTE_CONTROL = 1 << 9;
        /// Installer setup has been completed.
        SETUP_COMPLETED = 1 << 10;
        /// Is the master of its mesh.
        MESH_MASTER = 1 << 11;
        /// Has a notification pending.
        STATUS_NOTIFY = 1 << 12;
        /// Has a warning pending.
        STATUS_WARNING = 1 << 13;
        /// Has an error pending.
        STATUS_ERROR = 1 << 14;
        /// Is about to reboot.
        STATUS_REBOOT = 1 << 15;
        /// Is a member of a mesh.
        MESH_MEMBER = 1 << 16;
        /// Is an audio amplifier.
        AUDIO_AMPLIFIER = 1 << 17;
        /// Is still booting.
        BOOTING = 1 << 18;
        /// Is a management client rather than a device.
        MANAGER = 1 << 19;
        /// Is in power-save mode.
        STATUS_POWER_SAVE = 1 << 20;
        /// Supports meshing.
        MESH = 1 << 21;
        /// Is a multiviewer.
        MULTIVIEWER = 1 << 22;
        /// Has crashed since it last booted.
        STATUS_CRASHED = 1 << 23;
        /// Supports video walls.
        VIDEO_WALL = 1 << 24;
        /// Initialises the configuration it broadcasts.
        ///
        /// Firmware without this bit sends a device configuration built over
        /// uninitialised memory, so fields it did not mean to write carry junk.
        CONFIG_INITIALISED = 1 << 25;
        /// Receives one or more peers only by broadcast, because its multicast
        /// receive path is faulty.
        STATUS_MCAST_FAULT = 1 << 26;
        /// Powers its video processor down in power save. A device without it
        /// refuses to enter power save.
        POWER_SAVE = 1 << 27;
        /// Tags its uplink by the VLAN configuration it reports, and takes one
        /// written to it.
        VLAN = 1 << 28;
        /// Copies the overlay settings and logos of another unit when asked.
        OSD_SYNC = 1 << 29;
        /// Is downloading or installing a firmware or video processor
        /// upgrade. [`STATUS_REBOOT`](Self::STATUS_REBOOT) follows when it
        /// reboots to complete it.
        STATUS_UPGRADING = 1 << 30;
        /// Flips on every reboot, so a peer that sees it change knows the device restarted.
        BOOT_BIT = 1 << 31;
    }
}

bitmask! {
    /// What a OneIP device's video processor supports, as the device reports it
    /// in its configuration.
    ///
    /// Read-only, and a device's own: it fills the field in only on the frame
    /// describing itself, and leaves it zero on one it sends to configure
    /// another device. There is no write path.
    ///
    /// Bits are assigned by the video processor and only ever appended, so a
    /// bit this library has no name for is a later capability rather than an
    /// error. A device reports no features at all until its processor answers,
    /// and an older processor answers with none of the optional commands, so an
    /// empty mask is never reported as a capability set - see
    /// [`crate::Remote::v2ip_features`], which reports it as unknown instead.
    V2ipFpgaFeature: u64 {
        /// Applies a DSCP marking to the streams it sources.
        SOURCE_DSCP = 1 << 0;
        /// Reports the audio format arriving at its sink.
        SINK_AUDIO_FORMAT = 1 << 1;
        /// Places a tiling window on its sink.
        SINK_TILING_WINDOW = 1 << 2;
        /// Reports the state of its sink's overlay.
        SINK_OVERLAY_STATE = 1 << 3;
        /// Reports its sink's state.
        SINK_STATE = 1 << 4;
        /// Reports information about the stream its sink receives.
        SINK_STREAM_INFO = 1 << 5;
        /// Draws a test pattern, tone and lip-sync flash on its sink's output.
        SINK_TEST_PATTERN = 1 << 8;
    }
}

bitmask! {
    /// The device settings a OneIP configuration can carry, each on its own bit.
    ///
    /// The same bits serve as the settings a frame carries and as the values
    /// of the on/off ones among them. `IR_PROFILE` to `POWER_SAVE_SCHEDULE`
    /// carry a value elsewhere in the block and have no on/off value of their
    /// own.
    ///
    /// A device reports every setting it has, so one it never reports is one
    /// it does not have.
    V2ipDeviceSetting {
        /// Disables the decoder while the display is off.
        SINK_CHECK_POWER = 1 << 0;
        /// Disables the HDMI output while there is no signal.
        SINK_OFF_NO_SIGNAL = 1 << 1;
        /// Sends infrared modulated.
        IR_TX_MODULATED = 1 << 2;
        /// Lights the status LED.
        STATUS_LED = 1 << 3;
        /// Lights the network port LEDs.
        NETWORK_LED = 1 << 4;
        /// Runs the fan in quiet mode.
        FAN_QUIET = 1 << 5;
        /// Accepts CEC combo keys.
        CEC_COMBO_KEYS = 1 << 6;
        /// Accepts CEC combo keys for the device's own input.
        CEC_COMBO_INPUT = 1 << 7;
        /// The infrared profile of the device's global infrared port.
        IR_PROFILE = 1 << 8;
        /// The infrared profile of the output's infrared port.
        IR_PROFILE_SINK = 1 << 9;
        /// The infrared profiles stored on the device. Reported by the device
        /// itself and never written.
        IR_PROFILES = 1 << 10;
        /// The minutes a device stays idle before it powers down by itself.
        AUTO_POWER_SAVE = 1 << 11;
        /// The daily windows in which the device powers down.
        POWER_SAVE_SCHEDULE = 1 << 12;
        /// The device's clock has been set, on/off. Reported by the device
        /// itself and never written.
        CLOCK_SET = 1 << 13;
        /// The PTP mode the device runs.
        PTP_MODE = 1 << 14;
        /// The PTP domain the device runs in.
        PTP_DOMAIN = 1 << 15;
        /// The priority1 the device announces as a PTP grandmaster.
        PTP_PRIORITY1 = 1 << 16;
        /// Sends the source's audio as an AES67 flow, where the source allows.
        AES67 = 1 << 17;
        /// The power save level the device powers down to.
        POWER_SAVE_LEVEL = 1 << 18;
        /// The interval between the device's PTP announce messages.
        PTP_ANNOUNCE = 1 << 19;
        /// The interval between the device's PTP sync messages.
        PTP_SYNC = 1 << 20;
        /// The interval between the device's PTP delay requests.
        PTP_DELAY_REQ = 1 << 21;
        /// The announce intervals before the device drops a silent PTP master.
        PTP_ANNOUNCE_TIMEOUT = 1 << 22;
        /// The DSCP of the device's PTP messages.
        PTP_DSCP = 1 << 23;
        /// The minutes after which the device forgets a SAP announcement it
        /// heard.
        SAP_EXPIRE = 1 << 24;
        /// Listens for SAP announcements.
        SAP_LISTEN = 1 << 25;
        /// Sends SAP announcements.
        SAP_ANNOUNCE = 1 << 26;
        /// The seconds between the device's SAP announcements.
        SAP_INTERVAL = 1 << 27;
        /// Never powers down by itself: automatic power save and the power
        /// save schedule leave the device running, and switching this on
        /// wakes a device either has powered down. A power save request
        /// still powers it down. Writing
        /// [`POWER_SAVE_LEVEL`](Self::POWER_SAVE_LEVEL) switches it off.
        POWER_SAVE_DISABLED = 1 << 28;
        /// The TTL of the device's streams, video, audio and ancillary
        /// alike. Reported only by a device with an encoder.
        STREAM_TTL = 1 << 29;
        /// Connects to the cloud service. A device that does not report it
        /// connects regardless.
        CLOUD = 1 << 30;
        /// The angle the device's HDMI output is turned by. Reported only by
        /// a device with an HDMI output.
        SINK_ROTATION = 1 << 31;
    }
}

wire_enum! {
    /// When a OneIP output mixes 5.1 and 7.1 PCM down to stereo.
    V2ipDownmixMode: u8 {
        /// Never.
        OFF = 0;
        /// When the display's EDID lists two PCM channels or fewer.
        AUTO = 1;
        /// Always.
        ALWAYS = 2;
    }
}

wire_enum! {
    /// How loud a OneIP output's stereo downmix is.
    V2ipDownmixLevel: u8 {
        /// Scaled so the mix never clips.
        NORMALISED = 0;
        /// Unscaled, with dialogue at the level it has in stereo.
        UNSCALED = 1;
    }
}

wire_enum! {
    /// How far a OneIP device powers down in power save, each level saving more
    /// than the one before.
    V2ipPowerSaveLevel: u8 {
        /// No level.
        NONE = 0;
        /// Part of the video processing powers down.
        REDUCED = 1;
        /// All of the video processing powers down.
        LOW = 2;
        /// Everything but the network powers down.
        OFF = 3;
    }
}

bitmask! {
    /// What a unit offers at one power save level.
    UnitPowerSave: u8 {
        /// The unit can power down to this level.
        SUPPORTED = 1 << 0;
        /// A signal on the HDMI input wakes the unit.
        INPUT_WAKE = 1 << 1;
        /// The display powering on wakes the unit, and the unit can power it
        /// on.
        OUTPUT_WAKE = 1 << 2;
    }
}

wire_enum! {
    /// How a OneIP device takes part in PTP.
    V2ipPtpMode: u8 {
        /// No PTP.
        OFF = 0;
        /// Follows a grandmaster, and never becomes one.
        FOLLOWER = 1;
        /// Follows a grandmaster, or becomes one when there is none.
        AUTO = 2;
    }
}

wire_enum! {
    /// The test pattern a OneIP sink draws on its output.
    V2ipTestPattern: u8 {
        /// No pattern.
        OFF = 0;
        /// Colour bars.
        BARS = 1;
        /// One flat colour, the one the test card carries.
        FLAT = 2;
        /// A ramp.
        RAMP = 3;
        /// A grid.
        GRID = 4;
        /// A strip.
        STRIP = 5;
        /// A test card.
        CARD = 6;
    }
}

wire_enum! {
    /// The test tone a OneIP sink plays on its output.
    V2ipToneMode: u8 {
        /// No tone.
        OFF = 0;
        /// A continuous tone.
        CONTINUOUS = 1;
        /// A channel ident.
        IDENT = 2;
        /// A line-up tone, which needs two channels or more.
        LINEUP = 3;
        /// A beep on each lip-sync mark.
        BEEP = 4;
    }
}

bitmask! {
    /// What a OneIP sink reports about its test card.
    V2ipTestcardFlag: u8 {
        /// The sink can draw the test card.
        SUPPORTED = 1 << 0;
        /// The output shows the pattern.
        SHOWING = 1 << 1;
        /// The tone plays on the output.
        PLAYING = 1 << 2;
        /// A pattern change has yet to reach the video processor.
        PATTERN_PENDING = 1 << 3;
        /// A tone change has yet to reach the video processor.
        TONE_PENDING = 1 << 4;
        /// A lip-sync change has yet to reach the video processor.
        SYNC_PENDING = 1 << 5;
    }
}

bitmask! {
    /// The flags of what a OneIP output plays from a session description.
    V2ipSdpFlag: u8 {
        /// The output plays a stream from a description.
        ACTIVE = 1 << 0;
        /// And nothing else, as no video is routed to it.
        AUDIO_ONLY = 1 << 1;
        /// The clock mode and state hold the video processor's answer.
        CLOCK = 1 << 2;
    }
}

wire_enum! {
    /// The sample encoding of a stream a OneIP output plays from a description.
    V2ipSdpEncoding: u8 {
        /// None.
        NONE = 0;
        /// 16-bit linear PCM.
        L16 = 1;
        /// 24-bit linear PCM.
        L24 = 2;
        /// AM824.
        AM824 = 3;
    }
}

wire_enum! {
    /// Where a described stream's media clock comes from.
    V2ipSdpMediaClock: u8 {
        /// None given.
        NONE = 0;
        /// Direct, at an offset from the reference clock.
        DIRECT = 1;
        /// The sender's.
        SENDER = 2;
    }
}

wire_enum! {
    /// The reference clock a described stream names.
    V2ipSdpRefClock: u8 {
        /// None given.
        NONE = 0;
        /// PTP.
        PTP = 1;
        /// A traceable clock.
        TRACEABLE = 2;
        /// The sender's local MAC.
        LOCAL_MAC = 3;
    }
}

wire_enum! {
    /// What a OneIP output's video processor times a described stream by.
    V2ipSdpClockMode: u8 {
        /// Arrival.
        ARRIVAL = 0;
        /// PTP.
        PTP = 1;
        /// The sender.
        SENDER = 2;
    }
}

wire_enum! {
    /// How far a OneIP output's video processor has got with a described
    /// stream's timing.
    V2ipSdpClockState: u8 {
        /// Idle.
        IDLE = 0;
        /// Running its servo.
        SERVO = 1;
        /// Aligning.
        ALIGNING = 2;
        /// Timed.
        TIMED = 3;
        /// Parked.
        PARKED = 4;
        /// Playing untimed.
        UNTIMED = 5;
        /// The timing is not supported.
        UNSUPPORTED = 6;
    }
}

bitmask! {
    /// The flags of a unit's status report.
    UnitFlag {
        /// A changed setting takes effect after a reboot.
        REBOOT_REQUIRED = 1 << 0;
        /// A newer video processor image is available.
        FPGA_UPGRADE = 1 << 1;
        /// The IP configuration comes from DHCP.
        DHCP = 1 << 2;
        /// The IP configuration was changed through the mesh, and is reverted
        /// unless the mesh controller confirms it in time.
        NET_PENDING = 1 << 3;
        /// The fan runs in quiet mode.
        FAN_QUIET = 1 << 4;
        /// Powered by an external supply rather than PoE.
        EXTERNAL_PSU = 1 << 5;
        /// The video processor runs with its IP configured. Without it the
        /// encoder and decoder health carry nothing.
        FPGA_CONFIGURED = 1 << 6;
        /// The encoder is enabled.
        ENCODER_ACTIVE = 1 << 7;
        /// The decoder is enabled.
        DECODER_ACTIVE = 1 << 8;
        /// The decoder disables itself without a display.
        AUTO_DISABLE = 1 << 9;
        /// The decoder disabled itself without a display.
        AUTO_DISABLE_ACTIVE = 1 << 10;
        /// The HDMI output switches off without a signal.
        OFF_NO_SIGNAL = 1 << 11;
        /// The HDMI output switched off without a signal.
        OFF_NO_SIGNAL_ACTIVE = 1 << 12;
        /// Display power is checked before the decoder is enabled.
        POWER_CHECK = 1 << 13;
        /// The boot count is reported.
        BOOT_COUNT = 1 << 14;
        /// The fan speed is reported.
        FAN_SPEED = 1 << 15;
    }
}

wire_enum! {
    /// What a unit command asks of its unit.
    UnitCommandKind: u8 {
        /// Clear the current system status.
        CLEAR_STATUS = 0;
        /// Upgrade the video processor to the latest image.
        FPGA_UPGRADE = 1;
        /// Change the IP configuration.
        NET_CONFIG = 2;
        /// Keep a pending IP configuration.
        NET_CONFIRM = 3;
    }
}

bitmask! {
    /// The flags of a unit command.
    UnitCommandFlag: u8 {
        /// With [`UnitCommandKind::FPGA_UPGRADE`]: flash the image even if it
        /// is running already.
        FORCE = 1 << 0;
        /// With [`UnitCommandKind::NET_CONFIG`]: use DHCP, ignoring the
        /// addresses.
        DHCP = 1 << 1;
    }
}

bitmask! {
    /// The flags of a OneIP device's PTP state.
    V2ipPtpFlag: u8 {
        /// The device runs PTP and the block holds its state. A block without
        /// it holds nothing: the device has no PTP, or is in power save.
        VALID = 1 << 0;
        /// The device locked to a master since its PTP started, so its mesh
        /// holds a time.
        FOLLOWED = 1 << 1;
    }
}

wire_enum! {
    /// How far a OneIP device's PTP clock has locked to its master.
    V2ipPtpLock: u8 {
        /// Not locked.
        UNLOCKED = 0;
        /// Locking.
        LOCKING = 1;
        /// Locked.
        LOCKED = 2;
    }
}

bitmask! {
    /// The flags of a OneIP device's VLAN configuration.
    V2ipVlanFlag: u16 {
        /// The block carries a configuration. A block without it carries
        /// nothing, whatever its other bytes hold.
        VALID = 1 << 0;
        /// Untagged frames arriving on the uplink are dropped instead of
        /// reaching the device.
        TRUNK = 1 << 1;
        /// Reported by the device: the configuration is applied, and reverted
        /// unless the mesh controller confirms it. Never written.
        PENDING = 1 << 2;
        /// Sent by the mesh controller: a pending configuration it heard, which
        /// the device then keeps.
        CONFIRM = 1 << 3;
        /// Reported by the device: it has an SFP port. Never written.
        HAS_SFP = 1 << 4;
    }
}

impl V2ipDeviceSetting {
    /// The settings that are on or off, as opposed to carrying a value, and
    /// that can be written.
    pub const SWITCHES: Self = Self(
        0xFF | Self::AES67.0
            | Self::SAP_LISTEN.0
            | Self::SAP_ANNOUNCE.0
            | Self::POWER_SAVE_DISABLED.0
            | Self::CLOUD.0,
    );

    /// The settings only a device reports about itself, which no write
    /// carries.
    pub const REPORTED_ONLY: Self = Self(Self::IR_PROFILES.0 | Self::CLOCK_SET.0);

    /// The PTP settings.
    pub const PTP_SETTINGS: Self =
        Self(Self::PTP_MODE.0 | Self::PTP_DOMAIN.0 | Self::PTP_PRIORITY1.0);

    /// The PTP message settings.
    pub const PTP_MESSAGES: Self = Self(
        Self::PTP_ANNOUNCE.0
            | Self::PTP_SYNC.0
            | Self::PTP_DELAY_REQ.0
            | Self::PTP_ANNOUNCE_TIMEOUT.0
            | Self::PTP_DSCP.0,
    );

    /// The settings whose values a frame carries in a block of their own
    /// behind the rest, [`V2ipPtpMessages`](crate::V2ipPtpMessages): the PTP
    /// message settings, the two SAP values, the stream TTL and the output's
    /// rotation.
    pub const SETTINGS_BLOCK: Self = Self(
        Self::PTP_MESSAGES.0
            | Self::SAP_EXPIRE.0
            | Self::SAP_INTERVAL.0
            | Self::STREAM_TTL.0
            | Self::SINK_ROTATION.0,
    );

    /// These bits with every bit of `other` cleared.
    pub(crate) const fn without(self, other: Self) -> Self {
        Self(self.0 & !other.0)
    }
}

bitmask! {
    /// Capabilities of a single bay.
    BayFeatures {
        /// HDMI output.
        HDMI_OUT = 1 << 0;
        /// HDMI input.
        HDMI_IN = 1 << 1;
        /// Digital audio output.
        AUDIO_DIG_OUT = 1 << 2;
        /// Digital audio input.
        AUDIO_DIG_IN = 1 << 3;
        /// Analogue audio output.
        AUDIO_ANA_OUT = 1 << 4;
        /// Analogue audio input.
        AUDIO_ANA_IN = 1 << 5;
        /// Infrared input.
        IR_IN = 1 << 6;
        /// Infrared output.
        IR_OUT = 1 << 7;
        /// Amplified audio output.
        AUDIO_AMP_OUT = 1 << 8;
        /// Remote-control output.
        RC_OUT = 1 << 9;
        /// Remote-control input.
        RC_IN = 1 << 10;
        /// Dolby decoding.
        DOLBY = 1 << 11;
        /// Switches itself off when idle.
        AUTO_OFF = 1 << 12;
        /// Is a remote OneIP source.
        V2IP_SOURCE_REMOTE = 1 << 13;
        /// Is a remote OneIP sink.
        V2IP_SINK_REMOTE = 1 << 14;
        /// Is a local OneIP source.
        V2IP_SOURCE_LOCAL = 1 << 15;
        /// Is a local OneIP sink.
        V2IP_SINK_LOCAL = 1 << 16;
    }
}

bitmask! {
    /// Live status flags of a single bay.
    ///
    /// Bits 16-19 and 22-23 are bit-fields rather than flags; read them with
    /// [`BayStatus::rc_type`] and [`BayStatus::hdcp`].
    BayStatus {
        /// The bay reports a fault.
        FAULT = 1 << 0;
        /// The bay is hidden from the user interface.
        HIDDEN = 1 << 1;
        /// The amplifier zone the bay drives is powered on.
        POWERED = 1 << 2;
        /// A signal is present.
        SIGNAL_DETECTED = 1 << 3;
        /// Hot-plug detect is asserted.
        HPD_DETECTED = 1 << 4;
        /// The signal is scrambled.
        SIGNAL_SCRAMBLE = 1 << 5;
        /// An HDBaseT link is up.
        HDBT_CONNECTED = 1 << 6;
        /// A CEC device answered.
        CEC_DETECTED = 1 << 7;
        /// The attached device was powered on.
        POWERED_ON = 1 << 8;
        /// The attached device was powered off.
        POWERED_OFF = 1 << 9;
        /// Audio return over HDMI is active.
        AUDIO_ARC_HDMI = 1 << 10;
        /// Audio return over optical is active.
        AUDIO_ARC_OPTIC = 1 << 11;
        /// Audio return over analogue is active.
        AUDIO_ARC_ANALOG = 1 << 12;
        /// The bay is offline.
        OFFLINE = 1 << 13;
        /// The OneIP decoder is disabled.
        DECODER_DISABLE = 1 << 14;
        /// The OneIP encoder is disabled.
        ENCODER_DISABLE = 1 << 15;
        /// CEC is switched off for this bay.
        CEC_DISABLED = 1 << 20;
        /// The OneIP encoder reports an error.
        ENCODER_ERROR = 1 << 21;
        /// The bay's name was generated - a default, or taken from CEC or the
        /// EDID - rather than set by a user. A device that predates this bit
        /// never sets it, so a clear bit does not prove a user set the name.
        AUTO_NAME = 1 << 24;
    }
}

impl BayStatus {
    const RC_TYPE_SHIFT: u32 = 16;
    const RC_TYPE_MASK: u32 = 0xF << Self::RC_TYPE_SHIFT;
    const HDCP_SHIFT: u32 = 22;
    const HDCP_MASK: u32 = 0x3 << Self::HDCP_SHIFT;

    /// Extracts the remote-control type carried in bits 16-19.
    pub const fn rc_type(self) -> RcType {
        RcType(((self.0 & Self::RC_TYPE_MASK) >> Self::RC_TYPE_SHIFT) as u8)
    }

    /// Extracts the HDCP version carried in bits 22-23.
    pub const fn hdcp(self) -> u8 {
        ((self.0 & Self::HDCP_MASK) >> Self::HDCP_SHIFT) as u8
    }
}

bitmask! {
    /// Media carried by a virtual link.
    LinkFeature {
        /// Video over HDMI.
        VIDEO_HDMI = 1 << 0;
        /// Audio over optical.
        AUDIO_OPTICAL = 1 << 1;
        /// Audio over analogue.
        AUDIO_ANALOG = 1 << 2;
        /// Infrared.
        IR = 1 << 3;
        /// Remote control.
        RC = 1 << 4;
    }
}

wire_enum! {
    /// A remote-control action.
    RcAction: u16 {
        /// Toggle power.
        POWER_TOGGLE = 0;
        /// Power on.
        POWER_ON = 1;
        /// Power off.
        POWER_OFF = 2;
        /// Volume up.
        VOLUME_UP = 3;
        /// Volume down.
        VOLUME_DOWN = 4;
        /// Toggle mute.
        VOLUME_MUTE = 5;
    }
}

wire_enum! {
    /// A remote-control key code (CEC or IR).
    RcKey: u16 {
        /// Digit 0.
        NUM0 = 0;
        /// Digit 1.
        NUM1 = 1;
        /// Digit 2.
        NUM2 = 2;
        /// Digit 3.
        NUM3 = 3;
        /// Digit 4.
        NUM4 = 4;
        /// Digit 5.
        NUM5 = 5;
        /// Digit 6.
        NUM6 = 6;
        /// Digit 7.
        NUM7 = 7;
        /// Digit 8.
        NUM8 = 8;
        /// Digit 9.
        NUM9 = 9;
        /// Confirm the highlighted item.
        SELECT = 10;
        /// Go back one step.
        BACK = 11;
        /// Navigate up.
        UP = 12;
        /// Navigate down.
        DOWN = 13;
        /// Navigate left.
        LEFT = 14;
        /// Navigate right.
        RIGHT = 15;
        /// Open the main menu.
        MENU = 16;
        /// Open the content menu.
        CONTENT_MENU = 17;
        /// Next channel.
        CHANNEL_UP = 18;
        /// Previous channel.
        CHANNEL_DOWN = 19;
        /// Start playback.
        PLAY = 20;
        /// Pause playback.
        PAUSE = 21;
        /// Stop playback.
        STOP = 22;
        /// Start recording.
        RECORD = 23;
        /// Fast forward.
        FAST_FORWARD = 24;
        /// Rewind.
        REWIND = 25;
        /// Red colour key.
        RED = 26;
        /// Green colour key.
        GREEN = 27;
        /// Yellow colour key.
        YELLOW = 28;
        /// Blue colour key.
        BLUE = 29;
        /// Open help.
        HELP = 30;
        /// Show information.
        INFORMATION = 31;
        /// Open teletext.
        TEXT = 32;
        /// Open the programme guide.
        GUIDE = 33;
        /// Open video on demand.
        VIDEO_ON_DEMAND = 34;
        /// Return to the previous channel.
        PREVIOUS_CHANNEL = 80;
        /// Toggle 3D mode.
        MODE_3D = 81;
        /// Toggle subtitles.
        SUBTITLE = 82;
        /// Select an audio track.
        SOUND_SELECT = 83;
        /// Select an input.
        INPUT_SELECT = 84;
        /// Eject the medium.
        EJECT = 85;
        /// Next chapter.
        NEXT_CHAPTER = 86;
        /// Previous chapter.
        PREV_CHAPTER = 87;
        /// Open interactive services.
        INTERACTIVE = 128;
        /// Open search.
        SEARCH = 129;
        /// Sky home key.
        SKY = 130;
        /// Base of the range carrying a raw CEC user-control code.
        CUSTOM_CEC = 1280;
        /// Base of the range carrying a raw Sky key code.
        CUSTOM_SKY = 2048;
    }
}

wire_enum! {
    /// The remote-control protocol of a connected sink or source.
    RcType: u8 {
        /// Infrared.
        IR = 0;
        /// HDMI CEC.
        CEC = 1;
        /// Sky UK over IP.
        SKY_UK = 2;
        /// TiVo.
        TIVO = 3;
        /// Kodi.
        KODI = 4;
        /// Dish.
        DISH = 5;
        /// DirecTV.
        DIRECTV = 6;
        /// Another MX Remote device.
        MX_REMOTE = 7;
        /// Routed inside the device itself.
        INTERNAL = 8;
    }
}

impl fmt::Display for RcType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match *self {
            Self::IR => "IR",
            Self::CEC => "CEC",
            Self::SKY_UK => "Sky",
            Self::TIVO => "TiVo",
            Self::KODI => "Kodi",
            Self::DISH => "Dish",
            Self::DIRECTV => "DirecTV",
            Self::MX_REMOTE => "MX-Remote",
            Self::INTERNAL => "Internal",
            _ => "Unknown",
        };
        f.write_str(name)
    }
}

wire_enum! {
    /// An EDID preset selectable on an HDMI input.
    EdidProfile: u16 {
        /// 1080p with stereo audio.
        STEREO_1080P = 0;
        /// A fixed EDID stored on the device.
        FIXED = 1;
        /// 4K.
        UHD_4K = 2;
        /// 1080p with 5.1 audio.
        SURROUND51_1080P = 3;
        /// 720p.
        HD_720P = 4;
        /// 1080p with 7.1 audio.
        SURROUND71_1080P = 5;
        /// 4K with 7.1 audio.
        SURROUND71_4K = 6;
        /// 4K HDR with stereo audio.
        HDR_STEREO_4K = 7;
        /// 4K HDR with 7.1 audio.
        HDR_SURROUND71_4K = 8;
        /// 4K HDR, audio to the AVR only.
        HDR_AVR_ONLY_4K = 9;
        /// The lowest common denominator of the connected sinks.
        LOWEST_COMMON = 10;
        /// The lowest common denominator of every sink, connected or not.
        LOWEST_COMMON_ALL = 11;
        /// 4K HDR with Dolby Atmos.
        HDR_ATMOS_4K = 12;
        /// Copy the EDID of sink 1; the range runs to [`EdidProfile::SINK_32`].
        SINK_1 = 101;
        /// Copy the EDID of sink 32; the range starts at [`EdidProfile::SINK_1`].
        SINK_32 = 132;
        /// Base of the range carrying a user-supplied EDID.
        CUSTOM_0 = 500;
        /// The device reports no profile.
        UNKNOWN = 0xFFF;
    }
}

impl fmt::Display for EdidProfile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match *self {
            Self::STEREO_1080P => "1080p stereo",
            Self::FIXED => "fixed",
            Self::UHD_4K => "4K",
            Self::SURROUND51_1080P => "1080p 5.1",
            Self::HD_720P => "720p",
            Self::SURROUND71_1080P => "1080p 7.1",
            Self::SURROUND71_4K => "4K 7.1",
            Self::HDR_STEREO_4K => "4K HDR Stereo",
            Self::HDR_SURROUND71_4K => "4K HDR 7.1",
            Self::HDR_AVR_ONLY_4K => "4K HDR AVR",
            Self::LOWEST_COMMON => "lowest common denominator",
            Self::LOWEST_COMMON_ALL => "lowest common denominator (all sinks)",
            Self::HDR_ATMOS_4K => "4K HDR Dolby Atmos",
            _ => {
                if self.0 >= Self::SINK_1.0 && self.0 <= Self::SINK_32.0 {
                    return write!(f, "copy from sink #{}", self.0 - Self::SINK_1.0 + 1);
                }
                return write!(f, "custom #{}", self.0);
            }
        };
        f.write_str(name)
    }
}

wire_enum! {
    /// A firmware component.
    FirmwareType: u8 {
        /// The unit's own firmware.
        MCU = 0;
        /// The video processor's FPGA image.
        FPGA = 1;
        /// The video processor's RTOS image.
        RTOS = 2;
        /// The video processor's loader image.
        LOADER = 3;
        /// The unit's bootloader.
        BOOTLOADER = 4;
    }
}

impl fmt::Display for FirmwareType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match *self {
            Self::MCU => "MCU",
            Self::FPGA => "FPGA",
            Self::RTOS => "RTOS",
            Self::LOADER => "Loader",
            Self::BOOTLOADER => "Bootloader",
            _ => return write!(f, "firmware #{}", self.0),
        };
        f.write_str(name)
    }
}

wire_enum! {
    /// The negotiated speed of a network port.
    UtpLinkSpeed: u8 {
        /// The device reports no speed.
        UNKNOWN = 0;
        /// 10Mbit/s.
        SPEED_10M = 1;
        /// 100Mbit/s.
        SPEED_100M = 2;
        /// 200Mbit/s.
        SPEED_200M = 3;
        /// 1Gbit/s.
        SPEED_1G = 4;
    }
}

impl fmt::Display for UtpLinkSpeed {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match *self {
            Self::SPEED_10M => "10Mbit/s",
            Self::SPEED_100M => "100Mbit/s",
            Self::SPEED_200M => "200Mbit/s",
            Self::SPEED_1G => "1Gbit/s",
            _ => "Unknown",
        };
        f.write_str(name)
    }
}

wire_enum! {
    /// The window layout of a multiviewer.
    MultiviewerViewMode: u8 {
        /// The device reports no layout.
        UNKNOWN = 0;
        /// One full-screen window.
        SINGLE = 1;
        /// Picture in picture.
        PIP = 2;
        /// Two windows, large.
        TWO_SCREEN_LARGE = 3;
        /// Two windows, small.
        TWO_SCREEN_SMALL = 4;
        /// Three windows, large.
        THREE_SCREEN_LARGE = 5;
        /// Three windows, small.
        THREE_SCREEN_SMALL = 6;
        /// Four windows, equal size.
        FOUR_SCREEN_EQUAL = 7;
        /// Four windows, small.
        FOUR_SCREEN_SMALL = 8;
    }
}

wire_enum! {
    /// The corner a multiviewer places its picture-in-picture window in.
    MultiviewerPipPosition: u8 {
        /// The device reports no position.
        UNKNOWN = 0;
        /// Top left.
        LEFT_TOP = 1;
        /// Bottom left.
        LEFT_BOTTOM = 2;
        /// Top right.
        RIGHT_TOP = 3;
        /// Bottom right.
        RIGHT_BOTTOM = 4;
    }
}

wire_enum! {
    /// The size of a multiviewer's picture-in-picture window.
    MultiviewerPipSize: u8 {
        /// The device reports no size.
        UNKNOWN = 0;
        /// Small.
        SMALL = 1;
        /// Medium.
        MEDIUM = 2;
        /// Large.
        LARGE = 3;
    }
}

wire_enum! {
    /// The resolution and refresh rate a multiviewer drives its output at.
    MultiviewerOutputMode: u8 {
        /// The device reports no output mode.
        UNKNOWN = 0;
        /// 4096x2160p60.
        DCI4K_P60 = 1;
        /// 4096x2160p50.
        DCI4K_P50 = 2;
        /// 3840x2160p60.
        UHD_P60 = 3;
        /// 3840x2160p50.
        UHD_P50 = 4;
        /// 3840x2160p30.
        UHD_P30 = 5;
        /// 3840x2160p25.
        UHD_P25 = 6;
        /// 1920x1200p60, reduced blanking.
        WUXGA_P60_RB = 7;
        /// 1920x1080p60.
        HD1080_P60 = 8;
        /// 1920x1080p50.
        HD1080_P50 = 9;
        /// 1360x768p60.
        WXGA_P60 = 10;
        /// 1280x800p60.
        WXGA800_P60 = 11;
        /// 1280x720p60.
        HD720_P60 = 12;
        /// 1280x720p50.
        HD720_P50 = 13;
        /// 1024x768p60.
        XGA_P60 = 14;
    }
}

wire_enum! {
    /// The HDCP version a multiviewer output negotiates.
    MultiviewerHdcpMode: u8 {
        /// The device reports no HDCP mode.
        UNKNOWN = 0;
        /// HDCP 1.4.
        V14 = 1;
        /// HDCP 2.2.
        V22 = 2;
        /// Content protection off.
        OFF = 3;
    }
}

wire_enum! {
    /// The IT-content flag a multiviewer sets on its output.
    MultiviewerItcMode: u8 {
        /// The device reports no IT-content mode.
        UNKNOWN = 0;
        /// Video content.
        VIDEO = 1;
        /// PC content.
        PC = 2;
    }
}

wire_enum! {
    /// The EDID template a multiviewer presents to its sources.
    ///
    /// A template's name is the largest resolution it advertises and the audio
    /// format it declares support for.
    MultiviewerEdidTemplate: u8 {
        /// The device reports no template.
        EDID_UNKNOWN = 0;
        /// 4K2K60 4:4:4, stereo 2.0.
        EDID_4K2K60_444_STEREO = 1;
        /// 4K2K60 4:4:4, Dolby/DTS 5.1.
        EDID_4K2K60_444_DOLBY_DTS_51 = 2;
        /// 4K2K60 4:4:4, HD audio 7.1.
        EDID_4K2K60_444_HD_AUDIO_71 = 3;
        /// 4K2K30 4:4:4, stereo 2.0.
        EDID_4K2K30_444_STEREO = 4;
        /// 4K2K30 4:4:4, Dolby/DTS 5.1.
        EDID_4K2K30_444_DOLBY_DTS_51 = 5;
        /// 4K2K30 4:4:4, HD audio 7.1.
        EDID_4K2K30_444_HD_AUDIO_71 = 6;
        /// 1080p, stereo 2.0.
        EDID_1080P_STEREO = 7;
        /// 1080p, Dolby/DTS 5.1.
        EDID_1080P_DOLBY_DTS_51 = 8;
        /// 1080p, HD audio 7.1.
        EDID_1080P_HD_AUDIO_71 = 9;
        /// 1920x1200, stereo 2.0.
        EDID_1920X1200_STEREO = 10;
        /// 1680x1050, stereo 2.0.
        EDID_1680X1050_STEREO = 11;
        /// 1600x1200, stereo 2.0.
        EDID_1600X1200_STEREO = 12;
        /// 1440x900, stereo 2.0.
        EDID_1440X900_STEREO = 13;
        /// 1360x768, stereo 2.0.
        EDID_1360X768_STEREO = 14;
        /// 1280x1024, stereo 2.0.
        EDID_1280X1024_STEREO = 15;
        /// 1024x768, stereo 2.0.
        EDID_1024X768_STEREO = 16;
        /// 720p, stereo 2.0.
        EDID_720P_STEREO = 17;
        /// Whatever the display connected to the HDMI output presents. The
        /// template a multiviewer leaves the factory with.
        EDID_COPY_OUTPUT = 18;
        /// The EDID loaded onto the device.
        EDID_CUSTOM = 19;
    }
}

wire_enum! {
    /// The aspect ratio a multiviewer scales its windows to.
    MultiviewerAspectRatio: u8 {
        /// The device reports no aspect ratio.
        UNKNOWN = 0;
        /// Fill the window.
        FULL = 1;
        /// 16:9.
        RATIO_16_9 = 2;
    }
}

wire_enum! {
    /// A multiviewer setting that is on, off, or not reported.
    MultiviewerBool: u8 {
        /// Off.
        OFF = 0;
        /// On.
        ON = 1;
        /// The device reports no value.
        UNKNOWN = 0xFF;
    }
}

wire_enum! {
    /// One of a multiviewer's four inputs.
    ///
    /// The wire numbers the inputs from zero and this type from one, so that
    /// zero can mean "not reported" the way it does for every other
    /// multiviewer setting. So `to_wire` and `from_wire` carry this type's
    /// numbering rather than the wire's, and neither is the conversion to
    /// reach for when a raw multiviewer byte is what is in hand.
    MultiviewerSource: u8 {
        /// The device reports no source.
        UNKNOWN = 0;
        /// Input 1.
        INPUT_1 = 1;
        /// Input 2.
        INPUT_2 = 2;
        /// Input 3.
        INPUT_3 = 3;
        /// Input 4.
        INPUT_4 = 4;
    }
}

impl MultiviewerSource {
    /// Reads a zero-based wire value, mapping anything past input 4 to
    /// [`MultiviewerSource::UNKNOWN`].
    ///
    /// The firmware spells "not known" as 0xFF, which lands past input 4 and
    /// so needs no case of its own.
    pub(crate) const fn from_zero_based(value: u8) -> Self {
        if value > 3 {
            Self::UNKNOWN
        } else {
            Self(value + 1)
        }
    }

    /// The zero-based value the wire carries, or `None` for a source naming no
    /// input.
    ///
    /// A multiviewer reads zero as its first input, so there is no value that
    /// says "leave this alone": a request that cannot name an input has to be
    /// refused rather than sent.
    pub(crate) const fn to_zero_based(self) -> Option<u8> {
        match self.0 {
            1..=4 => Some(self.0 - 1),
            _ => None,
        }
    }
}

impl MultiviewerBool {
    /// Reads a wire value, mapping anything but 0 and 1 to
    /// [`MultiviewerBool::UNKNOWN`].
    pub(crate) const fn from_wire_tristate(value: u8) -> Self {
        if value > 1 {
            Self::UNKNOWN
        } else {
            Self(value)
        }
    }
}

wire_enum! {
    /// The colour space a OneIP output scales to.
    ///
    /// The field is four bits wide and only these four values are defined. A
    /// receiver passes the whole nibble to its validator, so a fifth value is
    /// dropped without a word rather than clamped to one of these.
    V2ipColourSpace: u8 {
        /// RGB.
        RGB = 0;
        /// YCbCr 4:4:4.
        YCBCR444 = 1;
        /// YCbCr 4:2:2.
        YCBCR422 = 2;
        /// YCbCr 4:2:0.
        YCBCR420 = 3;
    }
}

wire_enum! {
    /// The 2-byte `mxr_signal_type` carried in scaling configs and bay signal
    /// reports.
    ///
    /// Byte 0 is the CTA-861 short video descriptor, 0 when the signal is not
    /// HDMI. Byte 1 packs `color:4` in the low nibble, then `non_int:1` and
    /// `bpp:3` above it.
    MxrSignalType: u16 {
        /// No signal format was reported.
        NONE = 0;
    }
}

/// The bpp index a sender writes when it has no bit depth to report.
///
/// It sits outside the four indices that name a real depth, so an unset
/// format reads differently from every genuine one.
const SIG_BPP_UNSET: u16 = 5;

impl MxrSignalType {
    /// The CTA-861 short video descriptor, 0 when the signal is not HDMI.
    pub const fn svd(self) -> u8 {
        (self.0 & 0xFF) as u8
    }

    /// The colour space.
    pub const fn colour_space(self) -> u8 {
        ((self.0 >> 8) & 0xF) as u8
    }

    /// Whether the frame rate carries a 1000/1001 clock.
    pub const fn is_non_integer(self) -> bool {
        self.0 & (1 << 12) != 0
    }

    /// The raw bpp index as carried on the wire. The field is an index, not a
    /// bit depth; [`MxrSignalType::bpp`] converts it.
    pub const fn bpp_index(self) -> u8 {
        ((self.0 >> 13) & 0x7) as u8
    }

    /// The bit depth the bpp index stands for, `None` when unknown or unset.
    pub const fn bpp(self) -> Option<u8> {
        match self.bpp_index() {
            1 => Some(8),
            2 => Some(10),
            3 => Some(12),
            4 => Some(16),
            _ => None,
        }
    }

    /// Reports whether the word carries a signal format at all.
    ///
    /// A bay with nothing configured says so two ways. A sender that zeroes
    /// the word and stamps the unset bpp index leaves an index no real depth
    /// uses, and one that writes a plain zero leaves nothing at all. Neither
    /// is a format, and the svd and colour space beside them are not answers
    /// either: both read as zero, which is what this word says for "not HDMI"
    /// and "RGB" when it *is* set.
    pub const fn is_set(self) -> bool {
        self.0 != 0 && self.bpp_index() as u16 != SIG_BPP_UNSET
    }

    /// Builds the word from the fields a scaling write consumes.
    ///
    /// `non_int` is left clear: the receiving struct carries the bit, and the
    /// apply path does not read it.
    ///
    /// Building rather than editing is the point. A sink with no mode
    /// configured reports the word with the unset bpp index in it, so a caller
    /// that read that word back and filled in an svd would send an index no
    /// depth uses - which the receiver decodes to zero and rejects without
    /// answering.
    pub(crate) const fn from_parts(svd: u8, colour: u8, bpp_index: u8) -> Self {
        Self((svd as u16) | (((colour & 0xF) as u16) << 8) | (((bpp_index & 0x7) as u16) << 13))
    }

    /// The bpp index that stands for a bit depth, `None` for a depth no index
    /// names.
    ///
    /// Only the three depths a OneIP output stage accepts are here. Index 4
    /// names 16bpp, which [`MxrSignalType::bpp`] reads back from a device, but
    /// the output stage refuses it - so offering it as something to write would
    /// send a frame that is decoded cleanly and then dropped in silence.
    pub(crate) const fn bpp_index_for_depth(depth: u8) -> Option<u8> {
        match depth {
            8 => Some(1),
            10 => Some(2),
            12 => Some(3),
            _ => None,
        }
    }
}

impl fmt::Display for MxrSignalType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.is_set() {
            return f.write_str("unset");
        }
        match self.bpp() {
            Some(bpp) => write!(
                f,
                "svd {}, color {}, {}bpp",
                self.svd(),
                self.colour_space(),
                bpp
            ),
            None => write!(f, "svd {}, color {}", self.svd(), self.colour_space()),
        }
    }
}

bitmask! {
    /// The kinds of overlay setting an overlay sync copies.
    OsdSyncWhat: u8 {
        /// Captions.
        CAPTIONS = 1 << 0;
        /// Which logo is shown, and where.
        LOGO = 1 << 1;
        /// Background colours.
        BACKGROUNDS = 1 << 2;
        /// The logo images in the four slots.
        LOGO_SLOTS = 1 << 3;
    }
}

bitmask! {
    /// What a unit says of the message on its output.
    OsdMessageFlag: u8 {
        /// A message is set. In a write: show it, rather than take it down.
        SET = 1 << 0;
        /// The output shows it, rather than another message ahead of it.
        SHOWN = 1 << 1;
        /// The unit's video processor composes the message layer.
        SUPPORTED = 1 << 2;
        /// The unit did not take the write it answers, and reports the
        /// message it kept.
        REFUSED = 1 << 3;
    }
}

wire_enum! {
    /// Where a message stands against the unit's own messages: its upgrade,
    /// Locate Device and encrypted source messages.
    OsdMessagePriority: u8 {
        /// After them.
        LOW = 0;
        /// Between them.
        NORMAL = 1;
        /// Before them.
        TOP = 2;
    }
}

wire_enum! {
    /// How far a unit has got with taking an overlay sync.
    OsdSyncState: u8 {
        /// Taking it.
        TAKING = 0;
        /// Every setting written and committed.
        DONE = 1;
        /// Failed, for the reason the report gives.
        FAILED = 2;
        /// Kept for later, as the unit's video processor is off in power
        /// save.
        DEFERRED = 3;
    }
}

wire_enum! {
    /// The output size an overlay setting applies at.
    OsdSyncSize: u8 {
        /// Large outputs, which every value but `SMALL` also means.
        LARGE = 1;
        /// Small outputs.
        SMALL = 2;
    }
}

wire_enum! {
    /// How a caption's lines are aligned.
    OsdCaptionAlign: u8 {
        /// To the left.
        LEFT = 0;
        /// Centred.
        CENTRE = 1;
        /// To the right.
        RIGHT = 2;
        /// The layout's default.
        DEFAULT = 0xFF;
    }
}

wire_enum! {
    /// How a caption's text is read.
    OsdCaptionFormat: u8 {
        /// As plain text.
        PLAIN = 0;
        /// As Markdown.
        MARKDOWN = 1;
        /// The layout's default.
        DEFAULT = 0xFF;
    }
}

bitmask! {
    /// The caption fields an overlay setting gives. A field left out keeps
    /// what the unit has.
    OsdCaptionField: u16 {
        /// The font.
        FONT = 1 << 0;
        /// The size.
        SIZE = 1 << 1;
        /// The horizontal position.
        X = 1 << 2;
        /// The vertical position.
        Y = 1 << 3;
        /// The alignment.
        ALIGN = 1 << 4;
        /// The format.
        FORMAT = 1 << 5;
        /// The colour.
        COLOUR = 1 << 6;
        /// The wrap width.
        WRAP = 1 << 7;
        /// The text.
        TEXT = 1 << 8;
    }
}

bitmask! {
    /// The logo fields an overlay setting gives. A field left out keeps what
    /// the unit has.
    OsdLogoField: u16 {
        /// Which logo.
        LOGO = 1 << 0;
        /// The horizontal position.
        X = 1 << 1;
        /// The vertical position.
        Y = 1 << 2;
    }
}