Skip to main content

mx_remote/types/
status.rs

1// Author: Lars Op den Kamp (lars@opdenkamp-it.nl)
2// Copyright (c) 2026 Op den Kamp IT Solutions
3
4//! Device and bay state.
5
6use core::fmt;
7
8use crate::wire::{BayStatus, BayUid, FirmwareType, MxrSignalType};
9
10/// The high-level state of a device or bay on the network.
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub enum DeviceStatus {
13    /// Reachable and reporting.
14    #[default]
15    Online,
16    /// Has stopped answering.
17    Offline,
18    /// Announced a reboot.
19    Rebooting,
20    /// Still coming up.
21    Booting,
22    /// Present but not participating.
23    Inactive,
24}
25
26impl fmt::Display for DeviceStatus {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        f.write_str(match self {
29            Self::Online => "Online",
30            Self::Offline => "Offline",
31            Self::Rebooting => "Rebooting",
32            Self::Booting => "Booting",
33            Self::Inactive => "Inactive",
34        })
35    }
36}
37
38/// The CEC power state of a device connected to a bay.
39#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
40pub enum PowerStatus {
41    /// The device has not reported a power state.
42    #[default]
43    Unknown,
44    /// Powered on.
45    On,
46    /// Powered off.
47    Off,
48}
49
50impl fmt::Display for PowerStatus {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        f.write_str(match self {
53            Self::On => "on",
54            Self::Off => "off",
55            Self::Unknown => "unknown",
56        })
57    }
58}
59
60/// The connect / signal-detect state reported for a bay.
61#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
62pub enum ConnectStatus {
63    /// The device has not reported a connect state.
64    #[default]
65    Unknown,
66    /// Something is attached.
67    Connected,
68    /// Nothing is attached.
69    Disconnected,
70}
71
72impl fmt::Display for ConnectStatus {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        f.write_str(match self {
75            Self::Connected => "connected",
76            Self::Disconnected => "disconnected",
77            Self::Unknown => "unknown",
78        })
79    }
80}
81
82/// The visibility state of a bay.
83#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
84pub enum HiddenStatus {
85    /// The device has not reported a visibility.
86    #[default]
87    Unknown,
88    /// Hidden from the user interface.
89    Hidden,
90    /// Shown in the user interface.
91    Visible,
92}
93
94impl fmt::Display for HiddenStatus {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        f.write_str(match self {
97            Self::Hidden => "hidden",
98            Self::Visible => "visible",
99            Self::Unknown => "unknown",
100        })
101    }
102}
103
104/// The value that means "leave this one alone" in a volume or mute field.
105///
106/// It is not a level and not a mute state. A device reads it as "do not
107/// change", and the right channel additionally as "no such channel", so a
108/// decoder that surfaces it as a volume of 255 or as a muted pair reports a
109/// setting the sender was declining to touch.
110pub const VOLUME_UNCHANGED: u8 = 0xFF;
111
112/// The per-channel mute bitfield: bit 0 is left, bit 1 is right.
113#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
114pub struct MuteStatus(u8);
115
116impl MuteStatus {
117    /// Wraps the raw wire byte.
118    pub const fn from_wire(value: u8) -> Self {
119        Self(value)
120    }
121
122    /// Returns the raw wire byte.
123    pub const fn to_wire(self) -> u8 {
124        self.0
125    }
126
127    /// Whether the left channel is muted.
128    pub const fn left(self) -> bool {
129        self.0 & (1 << 0) != 0
130    }
131
132    /// Whether the right channel is muted.
133    pub const fn right(self) -> bool {
134        self.0 & (1 << 1) != 0
135    }
136
137    /// Whether either channel is muted.
138    pub const fn muted(self) -> bool {
139        self.0 != 0
140    }
141}
142
143/// The volume and mute state of a bay.
144///
145/// A `None` field is one the device did not report, which is distinct from a
146/// reported zero.
147#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
148pub struct VolumeMuteStatus {
149    /// Left channel volume, as a percentage.
150    pub volume_left: Option<u8>,
151    /// Right channel volume, as a percentage.
152    pub volume_right: Option<u8>,
153    /// Whether the left channel is muted.
154    pub muted_left: Option<bool>,
155    /// Whether the right channel is muted.
156    pub muted_right: Option<bool>,
157}
158
159impl VolumeMuteStatus {
160    /// The combined left/right volume percentage, or zero when neither channel
161    /// reported one.
162    pub fn volume(&self) -> u8 {
163        match (self.volume_left, self.volume_right) {
164            (Some(l), Some(r)) => ((u16::from(l) + u16::from(r)) / 2) as u8,
165            (Some(l), None) => l,
166            (None, Some(r)) => r,
167            (None, None) => 0,
168        }
169    }
170
171    /// The combined mute state, or `None` when neither channel reported one.
172    pub fn muted(&self) -> Option<bool> {
173        match (self.muted_left, self.muted_right) {
174            (None, None) => None,
175            (l, r) => Some(l.unwrap_or(false) || r.unwrap_or(false)),
176        }
177    }
178
179    /// Encodes the 3-byte `[volume_left, volume_right, muted]` field. A channel
180    /// that reported no volume is sent the combined one.
181    pub(crate) fn wire(&self) -> [u8; 3] {
182        [
183            self.volume_left.unwrap_or_else(|| self.volume()),
184            self.volume_right.unwrap_or_else(|| self.volume()),
185            self.muted_value(),
186        ]
187    }
188
189    /// Encodes the mute field: a per-channel bitmask, or
190    /// [`VOLUME_UNCHANGED`] where neither channel says.
191    ///
192    /// Not knowing is not the same as knowing it is unmuted. Zero is what a
193    /// device is told to unmute by, so sending it for a caller who named no
194    /// mute state would unmute a bay they only meant to set the volume of.
195    fn muted_value(&self) -> u8 {
196        match self.muted() {
197            None => VOLUME_UNCHANGED,
198            Some(false) => 0,
199            Some(true) => match (
200                self.muted_left.unwrap_or(false),
201                self.muted_right.unwrap_or(false),
202            ) {
203                (true, true) => 3,
204                (true, false) => 1,
205                _ => 2,
206            },
207        }
208    }
209}
210
211/// What a bay signal status report carries beyond the signal-detected flag and
212/// the human-readable signal type.
213#[derive(Clone, Copy, Debug, Default, PartialEq)]
214pub struct BaySignalDetails {
215    /// Frame rate in Hz, already corrected for a 1000/1001 clock.
216    pub frame_rate: f64,
217    /// TMDS clock rate in Hz.
218    pub tmds_clock: u32,
219    /// The bay status word from the report's bay block.
220    pub status: BayStatus,
221    /// The signal type the bay is scaling to.
222    pub scaling: MxrSignalType,
223    /// Video clock rate in Hz.
224    pub clock_rate: u32,
225    /// The audio alongside the video, absent when the report carried no audio
226    /// block.
227    pub audio: Option<BayAudioDetails>,
228}
229
230/// The audio a bay signal report describes.
231#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
232pub struct BayAudioDetails {
233    /// How the stream is encoded: 0 unknown, 1 L-PCM, 2 high bit rate.
234    pub format: u8,
235    /// Channel count.
236    pub channels: u8,
237    /// Sample rate in Hz.
238    pub sample_rate: u32,
239    /// The coding type the source claims in its CTA-861 audio infoframe, and
240    /// `None` where it sent no infoframe at all.
241    ///
242    /// The two are worth keeping apart: a source sending no infoframe leaves
243    /// this field zero, and zero is also what a source claiming "refer to the
244    /// stream header" writes into it.
245    pub coding: Option<u8>,
246}
247
248/// A firmware component reported by a device.
249#[derive(Clone, Debug, Default, PartialEq, Eq)]
250pub struct FirmwareVersion {
251    /// Which component this describes.
252    pub firmware_type: FirmwareType,
253    /// Build timestamp, in seconds since the Unix epoch.
254    pub timestamp: u32,
255    /// Human-readable version string.
256    pub version: String,
257    /// Source revision hash.
258    pub hash: u32,
259}
260
261impl fmt::Display for FirmwareVersion {
262    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
263        write!(
264            f,
265            "firmware {} version {} hash {}",
266            self.firmware_type, self.version, self.hash
267        )
268    }
269}
270
271/// Whether an output bay mirrors another device's output, and which one.
272///
273/// The default is "not mirroring".
274#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
275pub struct BayMirrorStatus {
276    /// The bay being mirrored, or `None` when this bay mirrors nothing.
277    pub target: Option<BayUid>,
278}
279
280impl BayMirrorStatus {
281    /// Reports whether this bay mirrors another.
282    pub const fn is_mirroring(&self) -> bool {
283        self.target.is_some()
284    }
285}
286
287/// One device in a topology report.
288#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
289pub struct TopologyEntry {
290    /// The device this entry describes.
291    pub uid: crate::wire::DeviceUid,
292    /// Bitmask of the devices it is connected to.
293    pub mask: u32,
294}
295
296/// The audio return channel a bay is carrying.
297#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
298pub enum ArcStatus {
299    /// No audio is being returned.
300    #[default]
301    Inactive,
302    /// Returned over HDMI.
303    Hdmi,
304    /// Returned over optical.
305    Optical,
306    /// Returned over analogue.
307    Analog,
308}
309
310impl fmt::Display for ArcStatus {
311    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
312        f.write_str(match self {
313            Self::Inactive => "Inactive",
314            Self::Hdmi => "HDMI",
315            Self::Optical => "optical",
316            Self::Analog => "analog",
317        })
318    }
319}