Skip to main content

mx_remote/wire/
enums.rs

1// Author: Lars Op den Kamp (lars@opdenkamp-it.nl)
2// Copyright (c) 2026 Op den Kamp IT Solutions
3
4//! Wire enumerations and bitmasks.
5//!
6//! Each type is a newtype over the integer that travels on the wire, with
7//! named constants rather than a closed set of variants. A value this library
8//! has no name for reaches the caller as it arrived: zero is a valid value for
9//! most of these, so a confidently wrong reading is worse than an unrecognised
10//! one.
11
12use core::fmt;
13use core::ops::{BitAnd, BitOr, BitOrAssign};
14
15/// Declares a bitmask newtype with the given named bit constants.
16///
17/// The representation defaults to `u32`; give it explicitly as `Name: u64` for
18/// a mask whose wire field is wider.
19macro_rules! bitmask {
20    (
21        $(#[$meta:meta])*
22        $name:ident { $( $(#[$cmeta:meta])* $cname:ident = $value:expr; )* }
23    ) => {
24        bitmask! {
25            $(#[$meta])*
26            $name: u32 { $( $(#[$cmeta])* $cname = $value; )* }
27        }
28    };
29    (
30        $(#[$meta:meta])*
31        $name:ident: $repr:ty { $( $(#[$cmeta:meta])* $cname:ident = $value:expr; )* }
32    ) => {
33        $(#[$meta])*
34        #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
35        pub struct $name($repr);
36
37        impl $name {
38            /// No bits set.
39            pub const NONE: Self = Self(0);
40
41            $( $(#[$cmeta])* pub const $cname: Self = Self($value); )*
42
43            /// Wraps a raw wire value, including bits this library has no name for.
44            pub const fn from_bits(bits: $repr) -> Self {
45                Self(bits)
46            }
47
48            /// Returns the raw wire value.
49            pub const fn bits(self) -> $repr {
50                self.0
51            }
52
53            /// Reports whether every bit in `other` is set.
54            pub const fn has(self, other: Self) -> bool {
55                self.0 & other.0 == other.0
56            }
57
58            /// Reports whether no bit is set.
59            pub const fn is_empty(self) -> bool {
60                self.0 == 0
61            }
62        }
63
64        impl BitOr for $name {
65            type Output = Self;
66            fn bitor(self, rhs: Self) -> Self {
67                Self(self.0 | rhs.0)
68            }
69        }
70
71        impl BitOrAssign for $name {
72            fn bitor_assign(&mut self, rhs: Self) {
73                self.0 |= rhs.0;
74            }
75        }
76
77        impl BitAnd for $name {
78            type Output = Self;
79            fn bitand(self, rhs: Self) -> Self {
80                Self(self.0 & rhs.0)
81            }
82        }
83
84        impl fmt::Display for $name {
85            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86                // Two hex digits per byte of the wire field, plus "0x".
87                write!(
88                    f,
89                    "{:#0width$x}",
90                    self.0,
91                    width = core::mem::size_of::<$repr>() * 2 + 2
92                )
93            }
94        }
95    };
96}
97
98/// Declares an enumeration newtype over `$repr` with the given named constants.
99macro_rules! wire_enum {
100    (
101        $(#[$meta:meta])*
102        $name:ident: $repr:ty { $( $(#[$cmeta:meta])* $cname:ident = $value:expr; )* }
103    ) => {
104        $(#[$meta])*
105        #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
106        pub struct $name($repr);
107
108        impl $name {
109            $( $(#[$cmeta])* pub const $cname: Self = Self($value); )*
110
111            /// Wraps a raw wire value, including one this library has no name for.
112            pub const fn from_wire(value: $repr) -> Self {
113                Self(value)
114            }
115
116            /// Returns the raw wire value.
117            pub const fn to_wire(self) -> $repr {
118                self.0
119            }
120        }
121    };
122}
123
124bitmask! {
125    /// Capabilities a device reports in its hello frame.
126    DeviceFeature {
127        /// Receives infrared.
128        IR_RX = 1 << 0;
129        /// Transmits infrared.
130        IR_TX = 1 << 1;
131        /// Speaks CEC.
132        CEC = 1 << 2;
133        /// Acts as a OneIP stream source.
134        V2IP_SOURCE = 1 << 3;
135        /// Acts as a OneIP stream sink.
136        V2IP_SINK = 1 << 4;
137        /// Routes video.
138        VIDEO_ROUTING = 1 << 5;
139        /// Routes audio.
140        AUDIO_ROUTING = 1 << 6;
141        /// Controls volume.
142        VOLUME_CONTROL = 1 << 7;
143        /// Supports audio return.
144        AUDIO_RETURN = 1 << 8;
145        /// Passes remote-control commands through.
146        REMOTE_CONTROL = 1 << 9;
147        /// Installer setup has been completed.
148        SETUP_COMPLETED = 1 << 10;
149        /// Is the master of its mesh.
150        MESH_MASTER = 1 << 11;
151        /// Has a notification pending.
152        STATUS_NOTIFY = 1 << 12;
153        /// Has a warning pending.
154        STATUS_WARNING = 1 << 13;
155        /// Has an error pending.
156        STATUS_ERROR = 1 << 14;
157        /// Is about to reboot.
158        STATUS_REBOOT = 1 << 15;
159        /// Is a member of a mesh.
160        MESH_MEMBER = 1 << 16;
161        /// Is an audio amplifier.
162        AUDIO_AMPLIFIER = 1 << 17;
163        /// Is still booting.
164        BOOTING = 1 << 18;
165        /// Is a management client rather than a device.
166        MANAGER = 1 << 19;
167        /// Is in power-save mode.
168        STATUS_POWER_SAVE = 1 << 20;
169        /// Supports meshing.
170        MESH = 1 << 21;
171        /// Is a multiviewer.
172        MULTIVIEWER = 1 << 22;
173        /// Has crashed since it last booted.
174        STATUS_CRASHED = 1 << 23;
175        /// Supports video walls.
176        VIDEO_WALL = 1 << 24;
177        /// Initialises the configuration it broadcasts.
178        ///
179        /// Firmware without this bit sends a device configuration built over
180        /// uninitialised memory, so fields it did not mean to write carry junk.
181        CONFIG_INITIALISED = 1 << 25;
182        /// Receives one or more peers only by broadcast, because its multicast
183        /// receive path is faulty.
184        STATUS_MCAST_FAULT = 1 << 26;
185        /// Powers its video processor down in power save. A device without it
186        /// refuses to enter power save.
187        POWER_SAVE = 1 << 27;
188        /// Tags its uplink by the VLAN configuration it reports, and takes one
189        /// written to it.
190        VLAN = 1 << 28;
191        /// Copies the overlay settings and logos of another unit when asked.
192        OSD_SYNC = 1 << 29;
193        /// Is downloading or installing a firmware or video processor
194        /// upgrade. [`STATUS_REBOOT`](Self::STATUS_REBOOT) follows when it
195        /// reboots to complete it.
196        STATUS_UPGRADING = 1 << 30;
197        /// Flips on every reboot, so a peer that sees it change knows the device restarted.
198        BOOT_BIT = 1 << 31;
199    }
200}
201
202bitmask! {
203    /// What a OneIP device's video processor supports, as the device reports it
204    /// in its configuration.
205    ///
206    /// Read-only, and a device's own: it fills the field in only on the frame
207    /// describing itself, and leaves it zero on one it sends to configure
208    /// another device. There is no write path.
209    ///
210    /// Bits are assigned by the video processor and only ever appended, so a
211    /// bit this library has no name for is a later capability rather than an
212    /// error. A device reports no features at all until its processor answers,
213    /// and an older processor answers with none of the optional commands, so an
214    /// empty mask is never reported as a capability set - see
215    /// [`crate::Remote::v2ip_features`], which reports it as unknown instead.
216    V2ipFpgaFeature: u64 {
217        /// Applies a DSCP marking to the streams it sources.
218        SOURCE_DSCP = 1 << 0;
219        /// Reports the audio format arriving at its sink.
220        SINK_AUDIO_FORMAT = 1 << 1;
221        /// Places a tiling window on its sink.
222        SINK_TILING_WINDOW = 1 << 2;
223        /// Reports the state of its sink's overlay.
224        SINK_OVERLAY_STATE = 1 << 3;
225        /// Reports its sink's state.
226        SINK_STATE = 1 << 4;
227        /// Reports information about the stream its sink receives.
228        SINK_STREAM_INFO = 1 << 5;
229        /// Draws a test pattern, tone and lip-sync flash on its sink's output.
230        SINK_TEST_PATTERN = 1 << 8;
231    }
232}
233
234bitmask! {
235    /// The device settings a OneIP configuration can carry, each on its own bit.
236    ///
237    /// The same bits serve as the settings a frame carries and as the values
238    /// of the on/off ones among them. `IR_PROFILE` to `POWER_SAVE_SCHEDULE`
239    /// carry a value elsewhere in the block and have no on/off value of their
240    /// own.
241    ///
242    /// A device reports every setting it has, so one it never reports is one
243    /// it does not have.
244    V2ipDeviceSetting {
245        /// Disables the decoder while the display is off.
246        SINK_CHECK_POWER = 1 << 0;
247        /// Disables the HDMI output while there is no signal.
248        SINK_OFF_NO_SIGNAL = 1 << 1;
249        /// Sends infrared modulated.
250        IR_TX_MODULATED = 1 << 2;
251        /// Lights the status LED.
252        STATUS_LED = 1 << 3;
253        /// Lights the network port LEDs.
254        NETWORK_LED = 1 << 4;
255        /// Runs the fan in quiet mode.
256        FAN_QUIET = 1 << 5;
257        /// Accepts CEC combo keys.
258        CEC_COMBO_KEYS = 1 << 6;
259        /// Accepts CEC combo keys for the device's own input.
260        CEC_COMBO_INPUT = 1 << 7;
261        /// The infrared profile of the device's global infrared port.
262        IR_PROFILE = 1 << 8;
263        /// The infrared profile of the output's infrared port.
264        IR_PROFILE_SINK = 1 << 9;
265        /// The infrared profiles stored on the device. Reported by the device
266        /// itself and never written.
267        IR_PROFILES = 1 << 10;
268        /// The minutes a device stays idle before it powers down by itself.
269        AUTO_POWER_SAVE = 1 << 11;
270        /// The daily windows in which the device powers down.
271        POWER_SAVE_SCHEDULE = 1 << 12;
272        /// The device's clock has been set, on/off. Reported by the device
273        /// itself and never written.
274        CLOCK_SET = 1 << 13;
275        /// The PTP mode the device runs.
276        PTP_MODE = 1 << 14;
277        /// The PTP domain the device runs in.
278        PTP_DOMAIN = 1 << 15;
279        /// The priority1 the device announces as a PTP grandmaster.
280        PTP_PRIORITY1 = 1 << 16;
281        /// Sends the source's audio as an AES67 flow, where the source allows.
282        AES67 = 1 << 17;
283        /// The level the device powers down to in power save. A device picks
284        /// it itself: the level that saves the most while its input and
285        /// display can still wake it. Written without
286        /// [`POWER_SAVE_DISABLED`](Self::POWER_SAVE_DISABLED), it switches
287        /// power save on, and the level written is ignored, except by a
288        /// device whose firmware predates picking it, which takes the level
289        /// as its setting.
290        POWER_SAVE_LEVEL = 1 << 18;
291        /// The interval between the device's PTP announce messages.
292        PTP_ANNOUNCE = 1 << 19;
293        /// The interval between the device's PTP sync messages.
294        PTP_SYNC = 1 << 20;
295        /// The interval between the device's PTP delay requests.
296        PTP_DELAY_REQ = 1 << 21;
297        /// The announce intervals before the device drops a silent PTP master.
298        PTP_ANNOUNCE_TIMEOUT = 1 << 22;
299        /// The DSCP of the device's PTP messages.
300        PTP_DSCP = 1 << 23;
301        /// The minutes after which the device forgets a SAP announcement it
302        /// heard.
303        SAP_EXPIRE = 1 << 24;
304        /// Listens for SAP announcements.
305        SAP_LISTEN = 1 << 25;
306        /// Sends SAP announcements.
307        SAP_ANNOUNCE = 1 << 26;
308        /// The seconds between the device's SAP announcements.
309        SAP_INTERVAL = 1 << 27;
310        /// Never powers down by itself: automatic power save and the power
311        /// save schedule leave the device running, and switching this on
312        /// wakes a device either has powered down. A power save request
313        /// still powers it down. Writing
314        /// [`POWER_SAVE_LEVEL`](Self::POWER_SAVE_LEVEL) switches it off.
315        POWER_SAVE_DISABLED = 1 << 28;
316        /// The TTL of the device's streams, video, audio and ancillary
317        /// alike. Reported only by a device with an encoder.
318        STREAM_TTL = 1 << 29;
319        /// Connects to the cloud service. A device that does not report it
320        /// connects regardless.
321        CLOUD = 1 << 30;
322        /// The angle the device's HDMI output is turned by. Reported only by
323        /// a device with an HDMI output.
324        SINK_ROTATION = 1 << 31;
325    }
326}
327
328wire_enum! {
329    /// When a OneIP output mixes 5.1 and 7.1 PCM down to stereo.
330    V2ipDownmixMode: u8 {
331        /// Never.
332        OFF = 0;
333        /// When the display's EDID lists two PCM channels or fewer.
334        AUTO = 1;
335        /// Always.
336        ALWAYS = 2;
337    }
338}
339
340wire_enum! {
341    /// How loud a OneIP output's stereo downmix is.
342    V2ipDownmixLevel: u8 {
343        /// Scaled so the mix never clips.
344        NORMALISED = 0;
345        /// Unscaled, with dialogue at the level it has in stereo.
346        UNSCALED = 1;
347    }
348}
349
350wire_enum! {
351    /// How far a OneIP device powers down in power save, each level saving more
352    /// than the one before.
353    V2ipPowerSaveLevel: u8 {
354        /// No level.
355        NONE = 0;
356        /// Part of the video processing powers down.
357        REDUCED = 1;
358        /// All of the video processing powers down.
359        LOW = 2;
360        /// Everything but the network powers down.
361        OFF = 3;
362    }
363}
364
365bitmask! {
366    /// What a unit offers at one power save level.
367    UnitPowerSave: u8 {
368        /// The unit can power down to this level.
369        SUPPORTED = 1 << 0;
370        /// A signal on the HDMI input wakes the unit.
371        INPUT_WAKE = 1 << 1;
372        /// The display powering on wakes the unit, and the unit can power it
373        /// on.
374        OUTPUT_WAKE = 1 << 2;
375    }
376}
377
378wire_enum! {
379    /// How a OneIP device takes part in PTP.
380    V2ipPtpMode: u8 {
381        /// No PTP.
382        OFF = 0;
383        /// Follows a grandmaster, and never becomes one.
384        FOLLOWER = 1;
385        /// Follows a grandmaster, or becomes one when there is none.
386        AUTO = 2;
387    }
388}
389
390wire_enum! {
391    /// The test pattern a OneIP sink draws on its output.
392    V2ipTestPattern: u8 {
393        /// No pattern.
394        OFF = 0;
395        /// Colour bars.
396        BARS = 1;
397        /// One flat colour, the one the test card carries.
398        FLAT = 2;
399        /// A ramp.
400        RAMP = 3;
401        /// A grid.
402        GRID = 4;
403        /// A strip.
404        STRIP = 5;
405        /// A test card.
406        CARD = 6;
407    }
408}
409
410wire_enum! {
411    /// The test tone a OneIP sink plays on its output.
412    V2ipToneMode: u8 {
413        /// No tone.
414        OFF = 0;
415        /// A continuous tone.
416        CONTINUOUS = 1;
417        /// A channel ident.
418        IDENT = 2;
419        /// A line-up tone, which needs two channels or more.
420        LINEUP = 3;
421        /// A beep on each lip-sync mark.
422        BEEP = 4;
423    }
424}
425
426bitmask! {
427    /// What a OneIP sink reports about its test card.
428    V2ipTestcardFlag: u8 {
429        /// The sink can draw the test card.
430        SUPPORTED = 1 << 0;
431        /// The output shows the pattern.
432        SHOWING = 1 << 1;
433        /// The tone plays on the output.
434        PLAYING = 1 << 2;
435        /// A pattern change has yet to reach the video processor.
436        PATTERN_PENDING = 1 << 3;
437        /// A tone change has yet to reach the video processor.
438        TONE_PENDING = 1 << 4;
439        /// A lip-sync change has yet to reach the video processor.
440        SYNC_PENDING = 1 << 5;
441    }
442}
443
444bitmask! {
445    /// The flags of what a OneIP output plays from a session description.
446    V2ipSdpFlag: u8 {
447        /// The output plays a stream from a description.
448        ACTIVE = 1 << 0;
449        /// And nothing else, as no video is routed to it.
450        AUDIO_ONLY = 1 << 1;
451        /// The clock mode and state hold the video processor's answer.
452        CLOCK = 1 << 2;
453    }
454}
455
456wire_enum! {
457    /// The sample encoding of a stream a OneIP output plays from a description.
458    V2ipSdpEncoding: u8 {
459        /// None.
460        NONE = 0;
461        /// 16-bit linear PCM.
462        L16 = 1;
463        /// 24-bit linear PCM.
464        L24 = 2;
465        /// AM824.
466        AM824 = 3;
467    }
468}
469
470wire_enum! {
471    /// Where a described stream's media clock comes from.
472    V2ipSdpMediaClock: u8 {
473        /// None given.
474        NONE = 0;
475        /// Direct, at an offset from the reference clock.
476        DIRECT = 1;
477        /// The sender's.
478        SENDER = 2;
479    }
480}
481
482wire_enum! {
483    /// The reference clock a described stream names.
484    V2ipSdpRefClock: u8 {
485        /// None given.
486        NONE = 0;
487        /// PTP.
488        PTP = 1;
489        /// A traceable clock.
490        TRACEABLE = 2;
491        /// The sender's local MAC.
492        LOCAL_MAC = 3;
493    }
494}
495
496wire_enum! {
497    /// What a OneIP output's video processor times a described stream by.
498    V2ipSdpClockMode: u8 {
499        /// Arrival.
500        ARRIVAL = 0;
501        /// PTP.
502        PTP = 1;
503        /// The sender.
504        SENDER = 2;
505    }
506}
507
508wire_enum! {
509    /// How far a OneIP output's video processor has got with a described
510    /// stream's timing.
511    V2ipSdpClockState: u8 {
512        /// Idle.
513        IDLE = 0;
514        /// Running its servo.
515        SERVO = 1;
516        /// Aligning.
517        ALIGNING = 2;
518        /// Timed.
519        TIMED = 3;
520        /// Parked.
521        PARKED = 4;
522        /// Playing untimed.
523        UNTIMED = 5;
524        /// The timing is not supported.
525        UNSUPPORTED = 6;
526    }
527}
528
529bitmask! {
530    /// The flags of a unit's status report.
531    UnitFlag {
532        /// A changed setting takes effect after a reboot.
533        REBOOT_REQUIRED = 1 << 0;
534        /// A newer video processor image is available.
535        FPGA_UPGRADE = 1 << 1;
536        /// The IP configuration comes from DHCP.
537        DHCP = 1 << 2;
538        /// The IP configuration was changed through the mesh, and is reverted
539        /// unless the mesh controller confirms it in time.
540        NET_PENDING = 1 << 3;
541        /// The fan runs in quiet mode.
542        FAN_QUIET = 1 << 4;
543        /// Powered by an external supply rather than PoE.
544        EXTERNAL_PSU = 1 << 5;
545        /// The video processor runs with its IP configured. Without it the
546        /// encoder and decoder health carry nothing.
547        FPGA_CONFIGURED = 1 << 6;
548        /// The encoder is enabled.
549        ENCODER_ACTIVE = 1 << 7;
550        /// The decoder is enabled.
551        DECODER_ACTIVE = 1 << 8;
552        /// The decoder disables itself without a display.
553        AUTO_DISABLE = 1 << 9;
554        /// The decoder disabled itself without a display.
555        AUTO_DISABLE_ACTIVE = 1 << 10;
556        /// The HDMI output switches off without a signal.
557        OFF_NO_SIGNAL = 1 << 11;
558        /// The HDMI output switched off without a signal.
559        OFF_NO_SIGNAL_ACTIVE = 1 << 12;
560        /// Display power is checked before the decoder is enabled.
561        POWER_CHECK = 1 << 13;
562        /// The boot count is reported.
563        BOOT_COUNT = 1 << 14;
564        /// The fan speed is reported.
565        FAN_SPEED = 1 << 15;
566    }
567}
568
569wire_enum! {
570    /// What a unit command asks of its unit.
571    UnitCommandKind: u8 {
572        /// Clear the current system status.
573        CLEAR_STATUS = 0;
574        /// Upgrade the video processor to the latest image.
575        FPGA_UPGRADE = 1;
576        /// Change the IP configuration.
577        NET_CONFIG = 2;
578        /// Keep a pending IP configuration.
579        NET_CONFIRM = 3;
580    }
581}
582
583bitmask! {
584    /// The flags of a unit command.
585    UnitCommandFlag: u8 {
586        /// With [`UnitCommandKind::FPGA_UPGRADE`]: flash the image even if it
587        /// is running already.
588        FORCE = 1 << 0;
589        /// With [`UnitCommandKind::NET_CONFIG`]: use DHCP, ignoring the
590        /// addresses.
591        DHCP = 1 << 1;
592    }
593}
594
595bitmask! {
596    /// The flags of a OneIP device's PTP state.
597    V2ipPtpFlag: u8 {
598        /// The device runs PTP and the block holds its state. A block without
599        /// it holds nothing: the device has no PTP, or is in power save.
600        VALID = 1 << 0;
601        /// The device locked to a master since its PTP started, so its mesh
602        /// holds a time.
603        FOLLOWED = 1 << 1;
604    }
605}
606
607wire_enum! {
608    /// How far a OneIP device's PTP clock has locked to its master.
609    V2ipPtpLock: u8 {
610        /// Not locked.
611        UNLOCKED = 0;
612        /// Locking.
613        LOCKING = 1;
614        /// Locked.
615        LOCKED = 2;
616    }
617}
618
619bitmask! {
620    /// The flags of a OneIP device's VLAN configuration.
621    V2ipVlanFlag: u16 {
622        /// The block carries a configuration. A block without it carries
623        /// nothing, whatever its other bytes hold.
624        VALID = 1 << 0;
625        /// Untagged frames arriving on the uplink are dropped instead of
626        /// reaching the device.
627        TRUNK = 1 << 1;
628        /// Reported by the device: the configuration is applied, and reverted
629        /// unless the mesh controller confirms it. Never written.
630        PENDING = 1 << 2;
631        /// Sent by the mesh controller: a pending configuration it heard, which
632        /// the device then keeps.
633        CONFIRM = 1 << 3;
634        /// Reported by the device: it has an SFP port. Never written.
635        HAS_SFP = 1 << 4;
636    }
637}
638
639impl V2ipDeviceSetting {
640    /// The settings that are on or off, as opposed to carrying a value, and
641    /// that can be written.
642    pub const SWITCHES: Self = Self(
643        0xFF | Self::AES67.0
644            | Self::SAP_LISTEN.0
645            | Self::SAP_ANNOUNCE.0
646            | Self::POWER_SAVE_DISABLED.0
647            | Self::CLOUD.0,
648    );
649
650    /// The settings only a device reports about itself, which no write
651    /// carries.
652    pub const REPORTED_ONLY: Self = Self(Self::IR_PROFILES.0 | Self::CLOCK_SET.0);
653
654    /// The PTP settings.
655    pub const PTP_SETTINGS: Self =
656        Self(Self::PTP_MODE.0 | Self::PTP_DOMAIN.0 | Self::PTP_PRIORITY1.0);
657
658    /// The PTP message settings.
659    pub const PTP_MESSAGES: Self = Self(
660        Self::PTP_ANNOUNCE.0
661            | Self::PTP_SYNC.0
662            | Self::PTP_DELAY_REQ.0
663            | Self::PTP_ANNOUNCE_TIMEOUT.0
664            | Self::PTP_DSCP.0,
665    );
666
667    /// The settings whose values a frame carries in a block of their own
668    /// behind the rest, [`V2ipPtpMessages`](crate::V2ipPtpMessages): the PTP
669    /// message settings, the two SAP values, the stream TTL and the output's
670    /// rotation.
671    pub const SETTINGS_BLOCK: Self = Self(
672        Self::PTP_MESSAGES.0
673            | Self::SAP_EXPIRE.0
674            | Self::SAP_INTERVAL.0
675            | Self::STREAM_TTL.0
676            | Self::SINK_ROTATION.0,
677    );
678
679    /// These bits with every bit of `other` cleared.
680    pub(crate) const fn without(self, other: Self) -> Self {
681        Self(self.0 & !other.0)
682    }
683}
684
685bitmask! {
686    /// Capabilities of a single bay.
687    BayFeatures {
688        /// HDMI output.
689        HDMI_OUT = 1 << 0;
690        /// HDMI input.
691        HDMI_IN = 1 << 1;
692        /// Digital audio output.
693        AUDIO_DIG_OUT = 1 << 2;
694        /// Digital audio input.
695        AUDIO_DIG_IN = 1 << 3;
696        /// Analogue audio output.
697        AUDIO_ANA_OUT = 1 << 4;
698        /// Analogue audio input.
699        AUDIO_ANA_IN = 1 << 5;
700        /// Infrared input.
701        IR_IN = 1 << 6;
702        /// Infrared output.
703        IR_OUT = 1 << 7;
704        /// Amplified audio output.
705        AUDIO_AMP_OUT = 1 << 8;
706        /// Remote-control output.
707        RC_OUT = 1 << 9;
708        /// Remote-control input.
709        RC_IN = 1 << 10;
710        /// Dolby decoding.
711        DOLBY = 1 << 11;
712        /// Switches itself off when idle.
713        AUTO_OFF = 1 << 12;
714        /// Is a remote OneIP source.
715        V2IP_SOURCE_REMOTE = 1 << 13;
716        /// Is a remote OneIP sink.
717        V2IP_SINK_REMOTE = 1 << 14;
718        /// Is a local OneIP source.
719        V2IP_SOURCE_LOCAL = 1 << 15;
720        /// Is a local OneIP sink.
721        V2IP_SINK_LOCAL = 1 << 16;
722    }
723}
724
725bitmask! {
726    /// Live status flags of a single bay.
727    ///
728    /// Bits 16-19 and 22-23 are bit-fields rather than flags; read them with
729    /// [`BayStatus::rc_type`] and [`BayStatus::hdcp`].
730    BayStatus {
731        /// The bay reports a fault.
732        FAULT = 1 << 0;
733        /// The bay is hidden from the user interface.
734        HIDDEN = 1 << 1;
735        /// The amplifier zone the bay drives is powered on.
736        POWERED = 1 << 2;
737        /// A signal is present.
738        SIGNAL_DETECTED = 1 << 3;
739        /// Hot-plug detect is asserted.
740        HPD_DETECTED = 1 << 4;
741        /// The signal is scrambled.
742        SIGNAL_SCRAMBLE = 1 << 5;
743        /// An HDBaseT link is up.
744        HDBT_CONNECTED = 1 << 6;
745        /// A CEC device answered.
746        CEC_DETECTED = 1 << 7;
747        /// The attached device was powered on.
748        POWERED_ON = 1 << 8;
749        /// The attached device was powered off.
750        POWERED_OFF = 1 << 9;
751        /// Audio return over HDMI is active.
752        AUDIO_ARC_HDMI = 1 << 10;
753        /// Audio return over optical is active.
754        AUDIO_ARC_OPTIC = 1 << 11;
755        /// Audio return over analogue is active.
756        AUDIO_ARC_ANALOG = 1 << 12;
757        /// The bay is offline.
758        OFFLINE = 1 << 13;
759        /// The OneIP decoder is disabled.
760        DECODER_DISABLE = 1 << 14;
761        /// The OneIP encoder is disabled.
762        ENCODER_DISABLE = 1 << 15;
763        /// CEC is switched off for this bay.
764        CEC_DISABLED = 1 << 20;
765        /// The OneIP encoder reports an error.
766        ENCODER_ERROR = 1 << 21;
767        /// The bay's name was generated - a default, or taken from CEC or the
768        /// EDID - rather than set by a user. A device that predates this bit
769        /// never sets it, so a clear bit does not prove a user set the name.
770        AUTO_NAME = 1 << 24;
771    }
772}
773
774impl BayStatus {
775    const RC_TYPE_SHIFT: u32 = 16;
776    const RC_TYPE_MASK: u32 = 0xF << Self::RC_TYPE_SHIFT;
777    const HDCP_SHIFT: u32 = 22;
778    const HDCP_MASK: u32 = 0x3 << Self::HDCP_SHIFT;
779
780    /// Extracts the remote-control type carried in bits 16-19.
781    pub const fn rc_type(self) -> RcType {
782        RcType(((self.0 & Self::RC_TYPE_MASK) >> Self::RC_TYPE_SHIFT) as u8)
783    }
784
785    /// Extracts the HDCP version carried in bits 22-23.
786    pub const fn hdcp(self) -> u8 {
787        ((self.0 & Self::HDCP_MASK) >> Self::HDCP_SHIFT) as u8
788    }
789}
790
791bitmask! {
792    /// Media carried by a virtual link.
793    LinkFeature {
794        /// Video over HDMI.
795        VIDEO_HDMI = 1 << 0;
796        /// Audio over optical.
797        AUDIO_OPTICAL = 1 << 1;
798        /// Audio over analogue.
799        AUDIO_ANALOG = 1 << 2;
800        /// Infrared.
801        IR = 1 << 3;
802        /// Remote control.
803        RC = 1 << 4;
804    }
805}
806
807wire_enum! {
808    /// A remote-control action.
809    RcAction: u16 {
810        /// Toggle power.
811        POWER_TOGGLE = 0;
812        /// Power on.
813        POWER_ON = 1;
814        /// Power off.
815        POWER_OFF = 2;
816        /// Volume up.
817        VOLUME_UP = 3;
818        /// Volume down.
819        VOLUME_DOWN = 4;
820        /// Toggle mute.
821        VOLUME_MUTE = 5;
822    }
823}
824
825wire_enum! {
826    /// A remote-control key code (CEC or IR).
827    RcKey: u16 {
828        /// Digit 0.
829        NUM0 = 0;
830        /// Digit 1.
831        NUM1 = 1;
832        /// Digit 2.
833        NUM2 = 2;
834        /// Digit 3.
835        NUM3 = 3;
836        /// Digit 4.
837        NUM4 = 4;
838        /// Digit 5.
839        NUM5 = 5;
840        /// Digit 6.
841        NUM6 = 6;
842        /// Digit 7.
843        NUM7 = 7;
844        /// Digit 8.
845        NUM8 = 8;
846        /// Digit 9.
847        NUM9 = 9;
848        /// Confirm the highlighted item.
849        SELECT = 10;
850        /// Go back one step.
851        BACK = 11;
852        /// Navigate up.
853        UP = 12;
854        /// Navigate down.
855        DOWN = 13;
856        /// Navigate left.
857        LEFT = 14;
858        /// Navigate right.
859        RIGHT = 15;
860        /// Open the main menu.
861        MENU = 16;
862        /// Open the content menu.
863        CONTENT_MENU = 17;
864        /// Next channel.
865        CHANNEL_UP = 18;
866        /// Previous channel.
867        CHANNEL_DOWN = 19;
868        /// Start playback.
869        PLAY = 20;
870        /// Pause playback.
871        PAUSE = 21;
872        /// Stop playback.
873        STOP = 22;
874        /// Start recording.
875        RECORD = 23;
876        /// Fast forward.
877        FAST_FORWARD = 24;
878        /// Rewind.
879        REWIND = 25;
880        /// Red colour key.
881        RED = 26;
882        /// Green colour key.
883        GREEN = 27;
884        /// Yellow colour key.
885        YELLOW = 28;
886        /// Blue colour key.
887        BLUE = 29;
888        /// Open help.
889        HELP = 30;
890        /// Show information.
891        INFORMATION = 31;
892        /// Open teletext.
893        TEXT = 32;
894        /// Open the programme guide.
895        GUIDE = 33;
896        /// Open video on demand.
897        VIDEO_ON_DEMAND = 34;
898        /// Return to the previous channel.
899        PREVIOUS_CHANNEL = 80;
900        /// Toggle 3D mode.
901        MODE_3D = 81;
902        /// Toggle subtitles.
903        SUBTITLE = 82;
904        /// Select an audio track.
905        SOUND_SELECT = 83;
906        /// Select an input.
907        INPUT_SELECT = 84;
908        /// Eject the medium.
909        EJECT = 85;
910        /// Next chapter.
911        NEXT_CHAPTER = 86;
912        /// Previous chapter.
913        PREV_CHAPTER = 87;
914        /// Open interactive services.
915        INTERACTIVE = 128;
916        /// Open search.
917        SEARCH = 129;
918        /// Sky home key.
919        SKY = 130;
920        /// Base of the range carrying a raw CEC user-control code.
921        CUSTOM_CEC = 1280;
922        /// Base of the range carrying a raw Sky key code.
923        CUSTOM_SKY = 2048;
924    }
925}
926
927wire_enum! {
928    /// The remote-control protocol of a connected sink or source.
929    RcType: u8 {
930        /// Infrared.
931        IR = 0;
932        /// HDMI CEC.
933        CEC = 1;
934        /// Sky UK over IP.
935        SKY_UK = 2;
936        /// TiVo.
937        TIVO = 3;
938        /// Kodi.
939        KODI = 4;
940        /// Dish.
941        DISH = 5;
942        /// DirecTV.
943        DIRECTV = 6;
944        /// Another MX Remote device.
945        MX_REMOTE = 7;
946        /// Routed inside the device itself.
947        INTERNAL = 8;
948    }
949}
950
951impl fmt::Display for RcType {
952    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
953        let name = match *self {
954            Self::IR => "IR",
955            Self::CEC => "CEC",
956            Self::SKY_UK => "Sky",
957            Self::TIVO => "TiVo",
958            Self::KODI => "Kodi",
959            Self::DISH => "Dish",
960            Self::DIRECTV => "DirecTV",
961            Self::MX_REMOTE => "MX-Remote",
962            Self::INTERNAL => "Internal",
963            _ => "Unknown",
964        };
965        f.write_str(name)
966    }
967}
968
969wire_enum! {
970    /// An EDID preset selectable on an HDMI input.
971    EdidProfile: u16 {
972        /// 1080p with stereo audio.
973        STEREO_1080P = 0;
974        /// A fixed EDID stored on the device.
975        FIXED = 1;
976        /// 4K.
977        UHD_4K = 2;
978        /// 1080p with 5.1 audio.
979        SURROUND51_1080P = 3;
980        /// 720p.
981        HD_720P = 4;
982        /// 1080p with 7.1 audio.
983        SURROUND71_1080P = 5;
984        /// 4K with 7.1 audio.
985        SURROUND71_4K = 6;
986        /// 4K HDR with stereo audio.
987        HDR_STEREO_4K = 7;
988        /// 4K HDR with 7.1 audio.
989        HDR_SURROUND71_4K = 8;
990        /// 4K HDR, audio to the AVR only.
991        HDR_AVR_ONLY_4K = 9;
992        /// The lowest common denominator of the connected sinks.
993        LOWEST_COMMON = 10;
994        /// The lowest common denominator of every sink, connected or not.
995        LOWEST_COMMON_ALL = 11;
996        /// 4K HDR with Dolby Atmos.
997        HDR_ATMOS_4K = 12;
998        /// Copy the EDID of sink 1; the range runs to [`EdidProfile::SINK_32`].
999        SINK_1 = 101;
1000        /// Copy the EDID of sink 32; the range starts at [`EdidProfile::SINK_1`].
1001        SINK_32 = 132;
1002        /// Base of the range carrying a user-supplied EDID.
1003        CUSTOM_0 = 500;
1004        /// The device reports no profile.
1005        UNKNOWN = 0xFFF;
1006    }
1007}
1008
1009impl fmt::Display for EdidProfile {
1010    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1011        let name = match *self {
1012            Self::STEREO_1080P => "1080p stereo",
1013            Self::FIXED => "fixed",
1014            Self::UHD_4K => "4K",
1015            Self::SURROUND51_1080P => "1080p 5.1",
1016            Self::HD_720P => "720p",
1017            Self::SURROUND71_1080P => "1080p 7.1",
1018            Self::SURROUND71_4K => "4K 7.1",
1019            Self::HDR_STEREO_4K => "4K HDR Stereo",
1020            Self::HDR_SURROUND71_4K => "4K HDR 7.1",
1021            Self::HDR_AVR_ONLY_4K => "4K HDR AVR",
1022            Self::LOWEST_COMMON => "lowest common denominator",
1023            Self::LOWEST_COMMON_ALL => "lowest common denominator (all sinks)",
1024            Self::HDR_ATMOS_4K => "4K HDR Dolby Atmos",
1025            _ => {
1026                if self.0 >= Self::SINK_1.0 && self.0 <= Self::SINK_32.0 {
1027                    return write!(f, "copy from sink #{}", self.0 - Self::SINK_1.0 + 1);
1028                }
1029                return write!(f, "custom #{}", self.0);
1030            }
1031        };
1032        f.write_str(name)
1033    }
1034}
1035
1036wire_enum! {
1037    /// A firmware component.
1038    FirmwareType: u8 {
1039        /// The unit's own firmware.
1040        MCU = 0;
1041        /// The video processor's FPGA image.
1042        FPGA = 1;
1043        /// The video processor's RTOS image.
1044        RTOS = 2;
1045        /// The video processor's loader image.
1046        LOADER = 3;
1047        /// The unit's bootloader.
1048        BOOTLOADER = 4;
1049    }
1050}
1051
1052impl fmt::Display for FirmwareType {
1053    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1054        let name = match *self {
1055            Self::MCU => "MCU",
1056            Self::FPGA => "FPGA",
1057            Self::RTOS => "RTOS",
1058            Self::LOADER => "Loader",
1059            Self::BOOTLOADER => "Bootloader",
1060            _ => return write!(f, "firmware #{}", self.0),
1061        };
1062        f.write_str(name)
1063    }
1064}
1065
1066wire_enum! {
1067    /// The negotiated speed of a network port.
1068    UtpLinkSpeed: u8 {
1069        /// The device reports no speed.
1070        UNKNOWN = 0;
1071        /// 10Mbit/s.
1072        SPEED_10M = 1;
1073        /// 100Mbit/s.
1074        SPEED_100M = 2;
1075        /// 200Mbit/s.
1076        SPEED_200M = 3;
1077        /// 1Gbit/s.
1078        SPEED_1G = 4;
1079    }
1080}
1081
1082impl fmt::Display for UtpLinkSpeed {
1083    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1084        let name = match *self {
1085            Self::SPEED_10M => "10Mbit/s",
1086            Self::SPEED_100M => "100Mbit/s",
1087            Self::SPEED_200M => "200Mbit/s",
1088            Self::SPEED_1G => "1Gbit/s",
1089            _ => "Unknown",
1090        };
1091        f.write_str(name)
1092    }
1093}
1094
1095wire_enum! {
1096    /// The window layout of a multiviewer.
1097    MultiviewerViewMode: u8 {
1098        /// The device reports no layout.
1099        UNKNOWN = 0;
1100        /// One full-screen window.
1101        SINGLE = 1;
1102        /// Picture in picture.
1103        PIP = 2;
1104        /// Two windows, large.
1105        TWO_SCREEN_LARGE = 3;
1106        /// Two windows, small.
1107        TWO_SCREEN_SMALL = 4;
1108        /// Three windows, large.
1109        THREE_SCREEN_LARGE = 5;
1110        /// Three windows, small.
1111        THREE_SCREEN_SMALL = 6;
1112        /// Four windows, equal size.
1113        FOUR_SCREEN_EQUAL = 7;
1114        /// Four windows, small.
1115        FOUR_SCREEN_SMALL = 8;
1116    }
1117}
1118
1119wire_enum! {
1120    /// The corner a multiviewer places its picture-in-picture window in.
1121    MultiviewerPipPosition: u8 {
1122        /// The device reports no position.
1123        UNKNOWN = 0;
1124        /// Top left.
1125        LEFT_TOP = 1;
1126        /// Bottom left.
1127        LEFT_BOTTOM = 2;
1128        /// Top right.
1129        RIGHT_TOP = 3;
1130        /// Bottom right.
1131        RIGHT_BOTTOM = 4;
1132    }
1133}
1134
1135wire_enum! {
1136    /// The size of a multiviewer's picture-in-picture window.
1137    MultiviewerPipSize: u8 {
1138        /// The device reports no size.
1139        UNKNOWN = 0;
1140        /// Small.
1141        SMALL = 1;
1142        /// Medium.
1143        MEDIUM = 2;
1144        /// Large.
1145        LARGE = 3;
1146    }
1147}
1148
1149wire_enum! {
1150    /// The resolution and refresh rate a multiviewer drives its output at.
1151    MultiviewerOutputMode: u8 {
1152        /// The device reports no output mode.
1153        UNKNOWN = 0;
1154        /// 4096x2160p60.
1155        DCI4K_P60 = 1;
1156        /// 4096x2160p50.
1157        DCI4K_P50 = 2;
1158        /// 3840x2160p60.
1159        UHD_P60 = 3;
1160        /// 3840x2160p50.
1161        UHD_P50 = 4;
1162        /// 3840x2160p30.
1163        UHD_P30 = 5;
1164        /// 3840x2160p25.
1165        UHD_P25 = 6;
1166        /// 1920x1200p60, reduced blanking.
1167        WUXGA_P60_RB = 7;
1168        /// 1920x1080p60.
1169        HD1080_P60 = 8;
1170        /// 1920x1080p50.
1171        HD1080_P50 = 9;
1172        /// 1360x768p60.
1173        WXGA_P60 = 10;
1174        /// 1280x800p60.
1175        WXGA800_P60 = 11;
1176        /// 1280x720p60.
1177        HD720_P60 = 12;
1178        /// 1280x720p50.
1179        HD720_P50 = 13;
1180        /// 1024x768p60.
1181        XGA_P60 = 14;
1182    }
1183}
1184
1185wire_enum! {
1186    /// The HDCP version a multiviewer output negotiates.
1187    MultiviewerHdcpMode: u8 {
1188        /// The device reports no HDCP mode.
1189        UNKNOWN = 0;
1190        /// HDCP 1.4.
1191        V14 = 1;
1192        /// HDCP 2.2.
1193        V22 = 2;
1194        /// Content protection off.
1195        OFF = 3;
1196    }
1197}
1198
1199wire_enum! {
1200    /// The IT-content flag a multiviewer sets on its output.
1201    MultiviewerItcMode: u8 {
1202        /// The device reports no IT-content mode.
1203        UNKNOWN = 0;
1204        /// Video content.
1205        VIDEO = 1;
1206        /// PC content.
1207        PC = 2;
1208    }
1209}
1210
1211wire_enum! {
1212    /// The EDID template a multiviewer presents to its sources.
1213    ///
1214    /// A template's name is the largest resolution it advertises and the audio
1215    /// format it declares support for.
1216    MultiviewerEdidTemplate: u8 {
1217        /// The device reports no template.
1218        EDID_UNKNOWN = 0;
1219        /// 4K2K60 4:4:4, stereo 2.0.
1220        EDID_4K2K60_444_STEREO = 1;
1221        /// 4K2K60 4:4:4, Dolby/DTS 5.1.
1222        EDID_4K2K60_444_DOLBY_DTS_51 = 2;
1223        /// 4K2K60 4:4:4, HD audio 7.1.
1224        EDID_4K2K60_444_HD_AUDIO_71 = 3;
1225        /// 4K2K30 4:4:4, stereo 2.0.
1226        EDID_4K2K30_444_STEREO = 4;
1227        /// 4K2K30 4:4:4, Dolby/DTS 5.1.
1228        EDID_4K2K30_444_DOLBY_DTS_51 = 5;
1229        /// 4K2K30 4:4:4, HD audio 7.1.
1230        EDID_4K2K30_444_HD_AUDIO_71 = 6;
1231        /// 1080p, stereo 2.0.
1232        EDID_1080P_STEREO = 7;
1233        /// 1080p, Dolby/DTS 5.1.
1234        EDID_1080P_DOLBY_DTS_51 = 8;
1235        /// 1080p, HD audio 7.1.
1236        EDID_1080P_HD_AUDIO_71 = 9;
1237        /// 1920x1200, stereo 2.0.
1238        EDID_1920X1200_STEREO = 10;
1239        /// 1680x1050, stereo 2.0.
1240        EDID_1680X1050_STEREO = 11;
1241        /// 1600x1200, stereo 2.0.
1242        EDID_1600X1200_STEREO = 12;
1243        /// 1440x900, stereo 2.0.
1244        EDID_1440X900_STEREO = 13;
1245        /// 1360x768, stereo 2.0.
1246        EDID_1360X768_STEREO = 14;
1247        /// 1280x1024, stereo 2.0.
1248        EDID_1280X1024_STEREO = 15;
1249        /// 1024x768, stereo 2.0.
1250        EDID_1024X768_STEREO = 16;
1251        /// 720p, stereo 2.0.
1252        EDID_720P_STEREO = 17;
1253        /// Whatever the display connected to the HDMI output presents. The
1254        /// template a multiviewer leaves the factory with.
1255        EDID_COPY_OUTPUT = 18;
1256        /// The EDID loaded onto the device.
1257        EDID_CUSTOM = 19;
1258    }
1259}
1260
1261wire_enum! {
1262    /// The aspect ratio a multiviewer scales its windows to.
1263    MultiviewerAspectRatio: u8 {
1264        /// The device reports no aspect ratio.
1265        UNKNOWN = 0;
1266        /// Fill the window.
1267        FULL = 1;
1268        /// 16:9.
1269        RATIO_16_9 = 2;
1270    }
1271}
1272
1273wire_enum! {
1274    /// A multiviewer setting that is on, off, or not reported.
1275    MultiviewerBool: u8 {
1276        /// Off.
1277        OFF = 0;
1278        /// On.
1279        ON = 1;
1280        /// The device reports no value.
1281        UNKNOWN = 0xFF;
1282    }
1283}
1284
1285wire_enum! {
1286    /// One of a multiviewer's four inputs.
1287    ///
1288    /// The wire numbers the inputs from zero and this type from one, so that
1289    /// zero can mean "not reported" the way it does for every other
1290    /// multiviewer setting. So `to_wire` and `from_wire` carry this type's
1291    /// numbering rather than the wire's, and neither is the conversion to
1292    /// reach for when a raw multiviewer byte is what is in hand.
1293    MultiviewerSource: u8 {
1294        /// The device reports no source.
1295        UNKNOWN = 0;
1296        /// Input 1.
1297        INPUT_1 = 1;
1298        /// Input 2.
1299        INPUT_2 = 2;
1300        /// Input 3.
1301        INPUT_3 = 3;
1302        /// Input 4.
1303        INPUT_4 = 4;
1304    }
1305}
1306
1307impl MultiviewerSource {
1308    /// Reads a zero-based wire value, mapping anything past input 4 to
1309    /// [`MultiviewerSource::UNKNOWN`].
1310    ///
1311    /// The firmware spells "not known" as 0xFF, which lands past input 4 and
1312    /// so needs no case of its own.
1313    pub(crate) const fn from_zero_based(value: u8) -> Self {
1314        if value > 3 {
1315            Self::UNKNOWN
1316        } else {
1317            Self(value + 1)
1318        }
1319    }
1320
1321    /// The zero-based value the wire carries, or `None` for a source naming no
1322    /// input.
1323    ///
1324    /// A multiviewer reads zero as its first input, so there is no value that
1325    /// says "leave this alone": a request that cannot name an input has to be
1326    /// refused rather than sent.
1327    pub(crate) const fn to_zero_based(self) -> Option<u8> {
1328        match self.0 {
1329            1..=4 => Some(self.0 - 1),
1330            _ => None,
1331        }
1332    }
1333}
1334
1335impl MultiviewerBool {
1336    /// Reads a wire value, mapping anything but 0 and 1 to
1337    /// [`MultiviewerBool::UNKNOWN`].
1338    pub(crate) const fn from_wire_tristate(value: u8) -> Self {
1339        if value > 1 {
1340            Self::UNKNOWN
1341        } else {
1342            Self(value)
1343        }
1344    }
1345}
1346
1347wire_enum! {
1348    /// The colour space a OneIP output scales to.
1349    ///
1350    /// The field is four bits wide and only these four values are defined. A
1351    /// receiver passes the whole nibble to its validator, so a fifth value is
1352    /// dropped without a word rather than clamped to one of these.
1353    V2ipColourSpace: u8 {
1354        /// RGB.
1355        RGB = 0;
1356        /// YCbCr 4:4:4.
1357        YCBCR444 = 1;
1358        /// YCbCr 4:2:2.
1359        YCBCR422 = 2;
1360        /// YCbCr 4:2:0.
1361        YCBCR420 = 3;
1362    }
1363}
1364
1365wire_enum! {
1366    /// The 2-byte `mxr_signal_type` carried in scaling configs and bay signal
1367    /// reports.
1368    ///
1369    /// Byte 0 is the CTA-861 short video descriptor, 0 when the signal is not
1370    /// HDMI. Byte 1 packs `color:4` in the low nibble, then `non_int:1` and
1371    /// `bpp:3` above it.
1372    MxrSignalType: u16 {
1373        /// No signal format was reported.
1374        NONE = 0;
1375    }
1376}
1377
1378/// The bpp index a sender writes when it has no bit depth to report.
1379///
1380/// It sits outside the four indices that name a real depth, so an unset
1381/// format reads differently from every genuine one.
1382const SIG_BPP_UNSET: u16 = 5;
1383
1384impl MxrSignalType {
1385    /// The CTA-861 short video descriptor, 0 when the signal is not HDMI.
1386    pub const fn svd(self) -> u8 {
1387        (self.0 & 0xFF) as u8
1388    }
1389
1390    /// The colour space.
1391    pub const fn colour_space(self) -> u8 {
1392        ((self.0 >> 8) & 0xF) as u8
1393    }
1394
1395    /// Whether the frame rate carries a 1000/1001 clock.
1396    pub const fn is_non_integer(self) -> bool {
1397        self.0 & (1 << 12) != 0
1398    }
1399
1400    /// The raw bpp index as carried on the wire. The field is an index, not a
1401    /// bit depth; [`MxrSignalType::bpp`] converts it.
1402    pub const fn bpp_index(self) -> u8 {
1403        ((self.0 >> 13) & 0x7) as u8
1404    }
1405
1406    /// The bit depth the bpp index stands for, `None` when unknown or unset.
1407    pub const fn bpp(self) -> Option<u8> {
1408        match self.bpp_index() {
1409            1 => Some(8),
1410            2 => Some(10),
1411            3 => Some(12),
1412            4 => Some(16),
1413            _ => None,
1414        }
1415    }
1416
1417    /// Reports whether the word carries a signal format at all.
1418    ///
1419    /// A bay with nothing configured says so two ways. A sender that zeroes
1420    /// the word and stamps the unset bpp index leaves an index no real depth
1421    /// uses, and one that writes a plain zero leaves nothing at all. Neither
1422    /// is a format, and the svd and colour space beside them are not answers
1423    /// either: both read as zero, which is what this word says for "not HDMI"
1424    /// and "RGB" when it *is* set.
1425    pub const fn is_set(self) -> bool {
1426        self.0 != 0 && self.bpp_index() as u16 != SIG_BPP_UNSET
1427    }
1428
1429    /// Builds the word from the fields a scaling write consumes.
1430    ///
1431    /// `non_int` is left clear: the receiving struct carries the bit, and the
1432    /// apply path does not read it.
1433    ///
1434    /// Building rather than editing is the point. A sink with no mode
1435    /// configured reports the word with the unset bpp index in it, so a caller
1436    /// that read that word back and filled in an svd would send an index no
1437    /// depth uses - which the receiver decodes to zero and rejects without
1438    /// answering.
1439    pub(crate) const fn from_parts(svd: u8, colour: u8, bpp_index: u8) -> Self {
1440        Self((svd as u16) | (((colour & 0xF) as u16) << 8) | (((bpp_index & 0x7) as u16) << 13))
1441    }
1442
1443    /// The bpp index that stands for a bit depth, `None` for a depth no index
1444    /// names.
1445    ///
1446    /// Only the three depths a OneIP output stage accepts are here. Index 4
1447    /// names 16bpp, which [`MxrSignalType::bpp`] reads back from a device, but
1448    /// the output stage refuses it - so offering it as something to write would
1449    /// send a frame that is decoded cleanly and then dropped in silence.
1450    pub(crate) const fn bpp_index_for_depth(depth: u8) -> Option<u8> {
1451        match depth {
1452            8 => Some(1),
1453            10 => Some(2),
1454            12 => Some(3),
1455            _ => None,
1456        }
1457    }
1458}
1459
1460impl fmt::Display for MxrSignalType {
1461    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1462        if !self.is_set() {
1463            return f.write_str("unset");
1464        }
1465        match self.bpp() {
1466            Some(bpp) => write!(
1467                f,
1468                "svd {}, color {}, {}bpp",
1469                self.svd(),
1470                self.colour_space(),
1471                bpp
1472            ),
1473            None => write!(f, "svd {}, color {}", self.svd(), self.colour_space()),
1474        }
1475    }
1476}
1477
1478bitmask! {
1479    /// The kinds of overlay setting an overlay sync copies.
1480    OsdSyncWhat: u8 {
1481        /// Captions.
1482        CAPTIONS = 1 << 0;
1483        /// Which logo is shown, and where.
1484        LOGO = 1 << 1;
1485        /// Background colours.
1486        BACKGROUNDS = 1 << 2;
1487        /// The logo images in the four slots.
1488        LOGO_SLOTS = 1 << 3;
1489    }
1490}
1491
1492bitmask! {
1493    /// What a unit says of the message on its output.
1494    OsdMessageFlag: u8 {
1495        /// A message is set. In a write: show it, rather than take it down.
1496        SET = 1 << 0;
1497        /// The output shows it, rather than another message ahead of it.
1498        SHOWN = 1 << 1;
1499        /// The unit's video processor composes the message layer.
1500        SUPPORTED = 1 << 2;
1501        /// The unit did not take the write it answers, and reports the
1502        /// message it kept.
1503        REFUSED = 1 << 3;
1504    }
1505}
1506
1507wire_enum! {
1508    /// Where a message stands against the unit's own messages: its upgrade,
1509    /// Locate Device and encrypted source messages.
1510    OsdMessagePriority: u8 {
1511        /// After them.
1512        LOW = 0;
1513        /// Between them.
1514        NORMAL = 1;
1515        /// Before them.
1516        TOP = 2;
1517    }
1518}
1519
1520wire_enum! {
1521    /// How far a unit has got with taking an overlay sync.
1522    OsdSyncState: u8 {
1523        /// Taking it.
1524        TAKING = 0;
1525        /// Every setting written and committed.
1526        DONE = 1;
1527        /// Failed, for the reason the report gives.
1528        FAILED = 2;
1529        /// Kept for later, as the unit's video processor is off in power
1530        /// save.
1531        DEFERRED = 3;
1532    }
1533}
1534
1535wire_enum! {
1536    /// The output size an overlay setting applies at.
1537    OsdSyncSize: u8 {
1538        /// Large outputs, which every value but `SMALL` also means.
1539        LARGE = 1;
1540        /// Small outputs.
1541        SMALL = 2;
1542    }
1543}
1544
1545wire_enum! {
1546    /// How a caption's lines are aligned.
1547    OsdCaptionAlign: u8 {
1548        /// To the left.
1549        LEFT = 0;
1550        /// Centred.
1551        CENTRE = 1;
1552        /// To the right.
1553        RIGHT = 2;
1554        /// The layout's default.
1555        DEFAULT = 0xFF;
1556    }
1557}
1558
1559wire_enum! {
1560    /// How a caption's text is read.
1561    OsdCaptionFormat: u8 {
1562        /// As plain text.
1563        PLAIN = 0;
1564        /// As Markdown.
1565        MARKDOWN = 1;
1566        /// The layout's default.
1567        DEFAULT = 0xFF;
1568    }
1569}
1570
1571bitmask! {
1572    /// The caption fields an overlay setting gives. A field left out keeps
1573    /// what the unit has.
1574    OsdCaptionField: u16 {
1575        /// The font.
1576        FONT = 1 << 0;
1577        /// The size.
1578        SIZE = 1 << 1;
1579        /// The horizontal position.
1580        X = 1 << 2;
1581        /// The vertical position.
1582        Y = 1 << 3;
1583        /// The alignment.
1584        ALIGN = 1 << 4;
1585        /// The format.
1586        FORMAT = 1 << 5;
1587        /// The colour.
1588        COLOUR = 1 << 6;
1589        /// The wrap width.
1590        WRAP = 1 << 7;
1591        /// The text.
1592        TEXT = 1 << 8;
1593    }
1594}
1595
1596bitmask! {
1597    /// The logo fields an overlay setting gives. A field left out keeps what
1598    /// the unit has.
1599    OsdLogoField: u16 {
1600        /// Which logo.
1601        LOGO = 1 << 0;
1602        /// The horizontal position.
1603        X = 1 << 1;
1604        /// The vertical position.
1605        Y = 1 << 2;
1606    }
1607}
1608
1609bitmask! {
1610    /// The first byte of a third-party vendor id, ahead of its three
1611    /// identifying bytes.
1612    ///
1613    /// Bits without a name are passed through as they arrived.
1614    ThirdPartyVendorFlag: u8 {
1615        /// The three bytes are an identifier the vendor chose, rather than its
1616        /// IEEE OUI.
1617        NOT_OUI = 1 << 0;
1618    }
1619}