Skip to main content

mx_remote/runtime/
mod.rs

1// Author: Lars Op den Kamp (lars@opdenkamp-it.nl)
2// Copyright (c) 2026 Op den Kamp IT Solutions
3
4//! The running client: the socket, the two threads that drive it, and the
5//! read surface over what they discover.
6
7mod control;
8mod info;
9mod logo_server;
10mod osd;
11mod schedule;
12
13#[cfg(test)]
14mod control_tests;
15#[cfg(test)]
16mod tests;
17
18// The reuseport check reads a Linux socket option, and pinning an addressless
19// interface is Linux-only, so the whole file is.
20#[cfg(all(test, target_os = "linux"))]
21mod socket;
22
23use std::io;
24use std::net::Ipv4Addr;
25use std::path::PathBuf;
26use std::sync::atomic::{AtomicBool, Ordering};
27use std::sync::{Arc, Condvar, Mutex, MutexGuard};
28use std::thread::JoinHandle;
29use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
30
31use crate::event::{Event, EventHandler};
32use crate::rx::process_frame;
33use crate::state::{Device, State, CONFIG_GRACE};
34use crate::types::*;
35use crate::wire::{
36    build_hello, op, Addressee, BayUid, Conn, DeviceFeature, DeviceUid, FirmwareType, Opcode,
37    SendError, Tx, V2ipFpgaFeature, MULTICAST_IP, MULTICAST_PORT, PROTOCOL_VERSION, VERSION,
38};
39
40pub use control::ControlError;
41pub use info::{BayInfo, DeviceInfo};
42use osd::OsdSender;
43use schedule::Schedule;
44
45/// The name this client advertises when the caller sets none.
46const DEFAULT_NAME: &str = "MXR Rust";
47
48/// The serial this client advertises.
49///
50/// A client is not a unit with a serial number, but the field is fixed-width
51/// and every device fills it, so it carries a constant rather than a blank.
52const CLIENT_SERIAL: &str = "P9SN00000000";
53
54/// The file the client's identifier is kept in, under the user's home
55/// directory. The identifier has to survive a restart, or every peer would see
56/// each run as a new client.
57const UID_FILE: &str = ".mxr-uid";
58
59/// Largest datagram the receive buffer accepts.
60const RECV_BUFFER: usize = 65535;
61
62/// How often the background thread re-examines the network.
63const PROBE_TICK: Duration = Duration::from_secs(1);
64
65/// How often that thread looks up from waiting to see whether it should stop.
66const SHUTDOWN_POLL: Duration = Duration::from_millis(50);
67
68/// Shortest gap between two discovery requests.
69const DISCOVER_INTERVAL: Duration = Duration::from_secs(5);
70
71/// How long a discover to every device stands in for asking any one of them.
72///
73/// Each device answers it with everything it holds, after a jitter of up to a
74/// few seconds, so asking one again inside this window repeats an answer
75/// already on its way.
76const DISCOVER_COVERS: Duration = Duration::from_secs(60);
77
78/// How a [`Remote`] finds the network.
79///
80/// [`Config::default`] discovers over multicast on the interface the host
81/// picks, which is the right answer on a single-homed machine and arbitrary on
82/// any other.
83#[derive(Clone, Debug, Default)]
84#[non_exhaustive]
85pub struct Config {
86    /// Where to send. Unset means the multicast group, or the interface's
87    /// broadcast address when [`Config::broadcast`] is set.
88    pub target_ip: Option<Ipv4Addr>,
89    /// UDP port. Unset means the default for the selected mode.
90    pub port: Option<u16>,
91    /// Selects the interface by address.
92    ///
93    /// It becomes both the multicast egress interface and the membership
94    /// interface, so it decides which NIC frames leave by and which one they
95    /// are accepted on. Getting it wrong on a multi-homed host is one-sided:
96    /// periodic broadcasts still arrive, so discovery looks healthy while
97    /// every request this client sends leaves by the wrong NIC and is never
98    /// answered.
99    pub local_ip: Option<Ipv4Addr>,
100    /// Selects the interface by name, taking precedence over
101    /// [`Config::local_ip`].
102    ///
103    /// An interface with no address of its own - a tagged VLAN - can only be
104    /// named this way, and only on Linux.
105    pub interface: Option<String>,
106    /// Use broadcast rather than multicast.
107    pub broadcast: bool,
108    /// The name this client advertises. Unset means `MXR Rust`.
109    pub name: Option<String>,
110    /// This client's identifier. Unset loads it from
111    /// [`Config::uid_path`], generating and storing one on first run.
112    pub uid: Option<DeviceUid>,
113    /// Where the identifier is kept. Unset means `.mxr-uid` in the user's home
114    /// directory.
115    pub uid_path: Option<PathBuf>,
116    /// The TCP port units fetch an overlay sync's logo images from. Unset
117    /// picks a free one when the first sync with images is sent.
118    pub logo_port: Option<u16>,
119}
120
121/// Everything the threads share.
122struct Shared {
123    uid: DeviceUid,
124    name: String,
125    handler: Arc<dyn EventHandler>,
126    state: Mutex<State>,
127    tx: Mutex<Tx>,
128    schedule: Mutex<Schedule>,
129    network: Mutex<Network>,
130    osd: Mutex<OsdSender>,
131    /// Woken when units ask for overlay settings.
132    osd_queued: Condvar,
133    logo_port: Option<u16>,
134    closing: AtomicBool,
135}
136
137/// The network parameters the socket was opened with, so it can be reopened.
138#[derive(Clone, Debug)]
139struct Network {
140    target_ip: Option<Ipv4Addr>,
141    port: Option<u16>,
142    local_ip: Option<Ipv4Addr>,
143    interface: Option<String>,
144    broadcast: bool,
145}
146
147impl Network {
148    /// The address to send to: what the caller asked for, else the multicast
149    /// group, else - in broadcast mode - the chosen interface's own broadcast
150    /// address.
151    fn target(&self) -> io::Result<Ipv4Addr> {
152        if let Some(ip) = self.target_ip {
153            return Ok(ip);
154        }
155        if !self.broadcast {
156            return Ok(MULTICAST_IP);
157        }
158        Ok(crate::wire::broadcast_address(self.local_ip).unwrap_or(MULTICAST_IP))
159    }
160
161    fn port(&self) -> u16 {
162        self.port.unwrap_or(if self.broadcast {
163            crate::wire::BROADCAST_PORT
164        } else {
165            MULTICAST_PORT
166        })
167    }
168
169    fn open(&self) -> io::Result<Conn> {
170        Conn::open(
171            self.target()?,
172            self.port(),
173            self.local_ip,
174            self.interface.as_deref(),
175        )
176    }
177}
178
179/// A client on the MX Remote network.
180///
181/// Create one with [`Remote::new`], then [`Remote::start`] it. Discovery runs
182/// on threads this client owns until [`Remote::close`], or until the `Remote`
183/// is dropped.
184pub struct Remote {
185    shared: Arc<Shared>,
186    workers: Mutex<Vec<JoinHandle<()>>>,
187}
188
189impl Remote {
190    /// Builds a client, loading or generating its identifier.
191    ///
192    /// Nothing is sent and no socket is opened until [`Remote::start`].
193    pub fn new(config: Config, handler: Arc<dyn EventHandler>) -> io::Result<Self> {
194        let uid = match config.uid {
195            Some(uid) => uid,
196            None => load_uid(config.uid_path.clone())?,
197        };
198        let name = config.name.unwrap_or_else(|| DEFAULT_NAME.to_owned());
199        Ok(Self {
200            shared: Arc::new(Shared {
201                uid,
202                name,
203                handler,
204                state: Mutex::new(State::new(uid)),
205                tx: Mutex::new(Tx::default()),
206                schedule: Mutex::new(Schedule::new()),
207                network: Mutex::new(Network {
208                    target_ip: config.target_ip,
209                    port: config.port,
210                    local_ip: config.local_ip,
211                    interface: config.interface,
212                    broadcast: config.broadcast,
213                }),
214                osd: Mutex::new(OsdSender::new()),
215                osd_queued: Condvar::new(),
216                logo_port: config.logo_port,
217                closing: AtomicBool::new(false),
218            }),
219            workers: Mutex::new(Vec::new()),
220        })
221    }
222
223    /// Opens the socket, announces this client and begins discovery.
224    ///
225    /// Returns once the receive thread is running; discovery continues in the
226    /// background.
227    pub fn start(&self) -> io::Result<()> {
228        let conn = lock(&self.shared.network).open()?;
229        lock(&self.shared.tx).set_conn(Some(conn));
230        self.shared.closing.store(false, Ordering::SeqCst);
231        self.spawn_workers()?;
232        self.shared.announce();
233        let _ = self.shared.discover();
234        Ok(())
235    }
236
237    /// Stops discovery, closes the socket and waits for the threads to finish.
238    ///
239    /// Calling it more than once, or before [`Remote::start`], does nothing.
240    pub fn close(&self) {
241        self.shared.closing.store(true, Ordering::SeqCst);
242        for worker in std::mem::take(&mut *lock(&self.workers)) {
243            let _ = worker.join();
244        }
245        // A closed client answers no unit, so it holds no sync either.
246        self.shared.drop_osd_sync();
247        // Only once no thread can still be reading from it: a descriptor
248        // released while another thread is parked on it can be reissued to
249        // something else before that thread returns.
250        lock(&self.shared.tx).set_conn(None);
251    }
252
253    fn spawn_workers(&self) -> io::Result<()> {
254        let mut workers = lock(&self.workers);
255        if !workers.is_empty() {
256            return Ok(());
257        }
258        for (name, body) in [
259            ("mxr-rx", Shared::receive_loop as fn(&Shared)),
260            ("mxr-probe", Shared::probe_loop as fn(&Shared)),
261            ("mxr-osd", Shared::osd_loop as fn(&Shared)),
262        ] {
263            let shared = Arc::clone(&self.shared);
264            workers.push(
265                std::thread::Builder::new()
266                    .name(name.to_owned())
267                    .spawn(move || body(&shared))?,
268            );
269        }
270        Ok(())
271    }
272
273    // ---- identity ----
274
275    /// This client's identifier, as peers see it.
276    pub fn uid(&self) -> DeviceUid {
277        self.shared.uid
278    }
279
280    /// The name this client advertises.
281    pub fn name(&self) -> &str {
282        &self.shared.name
283    }
284
285    /// The address frames are being sent to, once started.
286    pub fn target(&self) -> Option<std::net::SocketAddrV4> {
287        lock(&self.shared.tx).conn().map(|conn| conn.target())
288    }
289
290    // ---- reading the registry ----
291
292    /// Every device heard from, in no particular order.
293    pub fn devices(&self) -> Vec<DeviceUid> {
294        self.shared
295            .read(|state| state.devices.keys().copied().collect())
296    }
297
298    /// A snapshot of one device.
299    pub fn device(&self, uid: DeviceUid) -> Option<DeviceInfo> {
300        let now = Instant::now();
301        self.shared
302            .read(|state| state.device(uid).map(|d| DeviceInfo::of(d, now)))
303    }
304
305    /// The device with the given serial number.
306    pub fn device_by_serial(&self, serial: &str) -> Option<DeviceUid> {
307        self.shared
308            .read(|state| state.device_by_serial(serial).map(|d| d.uid))
309    }
310
311    /// Resolves a device from its dotted-hex identifier, falling back to a
312    /// serial-number match.
313    pub fn resolve_device(&self, name: &str) -> Option<DeviceUid> {
314        if let Ok(uid) = name.parse::<DeviceUid>() {
315            if self.shared.read(|state| state.device(uid).is_some()) {
316                return Some(uid);
317            }
318        }
319        self.device_by_serial(name)
320    }
321
322    /// A snapshot of one bay.
323    pub fn bay(&self, uid: BayUid) -> Option<BayInfo> {
324        self.shared
325            .read(|state| state.bay(uid).map(|bay| BayInfo::of(state, bay)))
326    }
327
328    /// The bay on `device` with the given port name, such as `Output 1`.
329    pub fn bay_by_name(&self, device: DeviceUid, port_name: &str) -> Option<BayUid> {
330        self.shared.read(|state| {
331            state
332                .device(device)?
333                .bay_by_name(port_name)
334                .map(crate::state::Bay::uid)
335        })
336    }
337
338    /// The source bay advertising the given multicast group, for the video or
339    /// the audio stream.
340    pub fn bay_by_stream_ip(&self, ip: Ipv4Addr, audio: bool) -> Option<BayUid> {
341        self.shared.read(|state| state.bay_by_stream_ip(ip, audio))
342    }
343
344    /// The V2IP streams a device advertises.
345    pub fn v2ip_sources(&self, uid: DeviceUid) -> Option<Vec<V2ipStreamSources>> {
346        self.shared
347            .read(|state| state.device(uid)?.v2ip_sources.clone())
348    }
349
350    /// A V2IP device's own encoder configuration.
351    pub fn v2ip_details(&self, uid: DeviceUid) -> Option<DeviceV2ipDetails> {
352        self.shared.read(|state| state.device(uid)?.v2ip_details)
353    }
354
355    /// The streams a V2IP sink is subscribed to.
356    pub fn v2ip_sink(&self, uid: DeviceUid) -> Option<DeviceV2ipSink> {
357        self.shared.read(|state| state.device(uid)?.v2ip_sink)
358    }
359
360    /// Transport statistics a V2IP device reports.
361    pub fn v2ip_stats(&self, uid: DeviceUid) -> Option<V2ipDeviceStats> {
362        self.shared.read(|state| state.device(uid)?.v2ip_stats)
363    }
364
365    /// What a V2IP device's video processor supports.
366    ///
367    /// `None` until the device has reported a non-empty mask about itself.
368    /// A device reports nothing at all while its processor has yet to answer,
369    /// and an older processor answers with none of the optional commands, so
370    /// "no features" and "not yet known" are the same bytes on the wire and
371    /// both read as `None` here. A mask that has arrived only gains bits.
372    pub fn v2ip_features(&self, uid: DeviceUid) -> Option<V2ipFpgaFeature> {
373        self.shared.read(|state| state.device(uid)?.v2ip_features)
374    }
375
376    /// A V2IP device's settings, `None` until it has reported any.
377    ///
378    /// A device reports only the settings it has, so one missing from
379    /// [`V2ipDeviceSettings::valid`] once the rest have arrived is one the
380    /// device does not have.
381    pub fn v2ip_device_settings(&self, uid: DeviceUid) -> Option<V2ipDeviceSettings> {
382        self.shared.read(|state| state.device(uid)?.v2ip_settings)
383    }
384
385    /// The VLAN configuration a V2IP device last reported about itself,
386    /// `None` until it has reported one.
387    pub fn v2ip_vlan(&self, uid: DeviceUid) -> Option<V2ipVlan> {
388        self.shared.read(|state| state.device(uid)?.v2ip_vlan)
389    }
390
391    /// What a V2IP output last reported playing from a session description,
392    /// `None` until it has reported.
393    pub fn v2ip_sink_sdp(&self, uid: DeviceUid) -> Option<V2ipSinkSdp> {
394        self.shared
395            .read(|state| state.device(uid)?.v2ip_sink_sdp.clone())
396    }
397
398    /// How a V2IP source unit's streams are encrypted, as it last reported
399    /// it, `None` until it has reported.
400    pub fn v2ip_privacy(&self, uid: DeviceUid) -> Option<V2ipPrivacy> {
401        self.shared.read(|state| state.device(uid)?.v2ip_privacy)
402    }
403
404    /// The overlay sync a management unit last sent, `None` until it has
405    /// sent one.
406    pub fn osd_sync(&self, uid: DeviceUid) -> Option<OsdSync> {
407        self.shared
408            .read(|state| state.device(uid)?.osd_sync.clone())
409    }
410
411    /// How a unit last reported getting on with an overlay sync, `None` until
412    /// it has reported. Match it to the sync by `seq` and the sync's source.
413    pub fn osd_sync_report(&self, uid: DeviceUid) -> Option<OsdSyncReport> {
414        self.shared
415            .read(|state| state.device(uid)?.osd_sync_report.clone())
416    }
417
418    /// The health and IP configuration a unit last reported, `None` until it
419    /// has reported them.
420    pub fn unit_status(&self, uid: DeviceUid) -> Option<UnitStatus> {
421        self.shared
422            .read(|state| state.device(uid)?.unit_status.clone())
423    }
424
425    /// The PTP state a V2IP device last reported about itself, `None` until
426    /// it has reported one, and while it has no PTP to report: none on its
427    /// video processor, or in power save.
428    pub fn v2ip_ptp(&self, uid: DeviceUid) -> Option<V2ipPtpState> {
429        self.shared.read(|state| state.device(uid)?.v2ip_ptp)
430    }
431
432    /// The test card a V2IP sink last reported, `None` until it has answered
433    /// [`Remote::request_v2ip_testcard`] or a change.
434    pub fn v2ip_testcard(&self, uid: DeviceUid) -> Option<V2ipTestcard> {
435        self.shared.read(|state| state.device(uid)?.v2ip_testcard)
436    }
437
438    /// The time zone a device announced for its mesh, `None` until it has.
439    ///
440    /// The mesh controller announces it with every periodic broadcast, empty
441    /// when it has none.
442    pub fn time_zone(&self, uid: DeviceUid) -> Option<TimeZone> {
443        self.shared
444            .read(|state| state.device(uid)?.time_zone.clone())
445    }
446
447    /// A device's clock as of now: the time it last announced, advanced by
448    /// how long ago that arrived. `None` until it has announced one.
449    ///
450    /// The mesh controller announces its clock with every periodic broadcast,
451    /// and only once it has been set.
452    pub fn device_clock(&self, uid: DeviceUid) -> Option<SystemTime> {
453        let (utc, at) = self.shared.read(|state| state.device(uid)?.clock)?;
454        let announced = UNIX_EPOCH.checked_add(Duration::from_secs(u64::from(utc)))?;
455        announced.checked_add(at.elapsed())
456    }
457
458    /// The video-wall tiling a V2IP device is configured for.
459    pub fn v2ip_tiling(&self, uid: DeviceUid) -> Option<V2ipTilingConfig> {
460        self.shared.read(|state| state.device(uid)?.tiling)
461    }
462
463    /// The audio endpoint tree a device exposes.
464    pub fn audio_endpoints(&self, uid: DeviceUid) -> Option<AudioEndpoints> {
465        self.shared.read(|state| state.device(uid)?.audio.clone())
466    }
467
468    /// The multiviewer layout a device is showing.
469    pub fn multiviewer_status(&self, uid: DeviceUid) -> Option<MultiviewerStatus> {
470        self.shared
471            .read(|state| state.device(uid)?.multiviewer.clone())
472    }
473
474    /// An amplifier's Dolby decoder settings.
475    pub fn dolby_settings(&self, uid: DeviceUid) -> Option<AmpDolbySettings> {
476        self.shared.read(|state| state.device(uid)?.dolby_settings)
477    }
478
479    /// A device's remote-control settings.
480    pub fn rc_settings(&self, uid: DeviceUid) -> Option<RcSettings> {
481        self.shared
482            .read(|state| state.device(uid)?.rc_settings.clone())
483    }
484
485    /// Every network port a device reports, in port order.
486    pub fn network_status(&self, uid: DeviceUid) -> Vec<NetworkPortStatus> {
487        self.shared.read(|state| {
488            state
489                .device(uid)
490                .map(|d| d.network.values().cloned().collect())
491                .unwrap_or_default()
492        })
493    }
494
495    /// The mesh topology a device reports.
496    pub fn topology(&self, uid: DeviceUid) -> Vec<TopologyEntry> {
497        self.shared.read(|state| {
498            state
499                .device(uid)
500                .map(|d| d.topology.clone())
501                .unwrap_or_default()
502        })
503    }
504
505    /// The EDID a device last reported: the display on its output, or the one
506    /// it presents to the source on its input.
507    ///
508    /// Filled in by a device's answer to [`Remote::request_edid`], and by any
509    /// answer to a peer's request that this client happened to hear.
510    pub fn edid(&self, uid: DeviceUid, output: bool) -> Option<Vec<u8>> {
511        self.shared
512            .read(|state| state.device(uid)?.edid(output).map(<[u8]>::to_vec))
513    }
514
515    /// How many frames from other senders have parsed since this client
516    /// started.
517    ///
518    /// It separates a mesh with nothing on it from an interface nothing is on:
519    /// a client that has discovered no device but is counting frames is
520    /// hearing traffic it cannot get answers from, which on a multi-homed host
521    /// is what a wrong [`Config::local_ip`] looks like. Frames this client
522    /// sent are not counted, because the host loops its own multicast back
523    /// whichever interface was selected.
524    pub fn frames_received(&self) -> u64 {
525        self.shared.read(|state| state.frames_received)
526    }
527
528    /// Every firmware image a device reports a version for.
529    pub fn firmware(&self, uid: DeviceUid) -> Vec<(FirmwareType, FirmwareVersion)> {
530        self.shared.read(|state| {
531            state
532                .device(uid)
533                .map(|d| d.firmware.iter().map(|(k, v)| (*k, v.clone())).collect())
534                .unwrap_or_default()
535        })
536    }
537
538    // ---- reconfiguring ----
539
540    /// Changes the interface and the multicast/broadcast mode while running,
541    /// reopening the socket when either differs from what is in use.
542    pub fn update_config(&self, local_ip: Option<Ipv4Addr>, broadcast: bool) -> io::Result<()> {
543        let network = {
544            let mut network = lock(&self.shared.network);
545            if network.local_ip == local_ip && network.broadcast == broadcast {
546                return Ok(());
547            }
548            network.local_ip = local_ip;
549            network.broadcast = broadcast;
550            network.clone()
551        };
552        // Opened before the old one is dropped, so a bind that fails leaves the
553        // client on the socket it had rather than on none.
554        let conn = network.open()?;
555        lock(&self.shared.tx).set_conn(Some(conn));
556        self.shared.announce();
557        let _ = self.shared.discover();
558        Ok(())
559    }
560
561    /// Asks every device on the network to announce itself.
562    pub fn discover(&self) -> Result<(), SendError> {
563        self.shared.discover()
564    }
565}
566
567impl Drop for Remote {
568    fn drop(&mut self) {
569        self.close();
570    }
571}
572
573impl Shared {
574    /// Reads the registry.
575    fn read<R>(&self, f: impl FnOnce(&State) -> R) -> R {
576        f(&lock(&self.state))
577    }
578
579    /// Mutates the registry, then delivers what changed.
580    ///
581    /// The queue is drained after the lock is released, so an event handler
582    /// may call back into the library.
583    fn mutate<R>(&self, f: impl FnOnce(&mut State, &mut Vec<Event>) -> R) -> R {
584        let mut events = Vec::new();
585        let result = {
586            let mut state = lock(&self.state);
587            let result = f(&mut state, &mut events);
588            state.mirror_mesh_events(&mut events);
589            result
590        };
591        self.dispatch(events);
592        result
593    }
594
595    fn dispatch(&self, events: Vec<Event>) {
596        for event in events {
597            event.dispatch(&*self.handler);
598        }
599    }
600
601    /// Decodes one datagram and delivers what it changed.
602    ///
603    /// This is the receive entry point. Keeping it distinct from the decode
604    /// below matters even though the wrapper is thin: announcing is driven by a
605    /// clock, and the one frame that may add a hello here is a ping addressed
606    /// to this client, which asks for exactly that.
607    fn process_datagram(&self, data: &[u8], from: Ipv4Addr) {
608        let (events, hello_requested, state_requests, osd_requests) = {
609            let mut state = lock(&self.state);
610            let events = process_frame(&mut state, data, Some(from), Instant::now());
611            (
612                events,
613                std::mem::take(&mut state.hello_requested),
614                std::mem::take(&mut state.state_requests),
615                std::mem::take(&mut state.osd_requests),
616            )
617        };
618        self.dispatch(events);
619        if hello_requested {
620            self.announce();
621        }
622        for device in state_requests {
623            self.request_state(device);
624        }
625        if !osd_requests.is_empty() {
626            self.answer_osd_requests(osd_requests);
627        }
628    }
629
630    /// Sends a frame, refusing one the addressee cannot decode.
631    fn send(&self, to: &Addressee, opcode: Opcode, payload: &[u8]) -> Result<usize, SendError> {
632        lock(&self.tx).send(to, self.uid, opcode, payload)
633    }
634
635    fn discover(&self) -> Result<(), SendError> {
636        lock(&self.schedule).discovered(Instant::now());
637        self.send(&Addressee::Broadcast, op::SYS_DISCOVER, &[])?;
638        Ok(())
639    }
640
641    /// Asks one device for everything it holds, with a discover naming it.
642    ///
643    /// Skipped while a discover to every device is recent enough to have
644    /// covered it. A receiver that predates the named form answers it as a
645    /// discover to every device, so each one asked costs the whole mesh's
646    /// state on such a network.
647    fn request_state(&self, device: DeviceUid) {
648        if lock(&self.schedule).discover_covers(Instant::now()) {
649            return;
650        }
651        let _ = self.send(&Addressee::Broadcast, op::SYS_DISCOVER, device.as_bytes());
652    }
653
654    /// Announces this client, and re-arms the announcement timer only once the
655    /// frame is away.
656    ///
657    /// The firmware resets its own hello timeout inside the branch where the
658    /// transmit succeeded. A send that fails is then retried on the next tick
659    /// rather than costing a whole interval of silence, which matters most at
660    /// startup and after a network blip: exactly when being heard is worth the
661    /// most.
662    ///
663    /// It announces `CONFIG_INITIALISED` because a unit uses the scaling
664    /// fields of a 0x3C write only from a sender that does, and this client
665    /// zeroes every field it does not set.
666    ///
667    /// While it holds an overlay sync it also announces itself as the mesh's
668    /// overlay sync manager, which is what has the controller leave units'
669    /// requests for a new sync to it.
670    fn announce(&self) {
671        let mut features = DeviceFeature::MANAGER | DeviceFeature::CONFIG_INITIALISED;
672        if lock(&self.osd).holds() {
673            features |= DeviceFeature::OSD_SYNC;
674        }
675        let payload = build_hello(
676            PROTOCOL_VERSION,
677            &self.name,
678            CLIENT_SERIAL,
679            VERSION,
680            features.bits(),
681        );
682        match self.send(&Addressee::Broadcast, op::SYS_HELLO, &payload) {
683            Ok(n) if n > 0 => lock(&self.schedule).announced(Instant::now()),
684            _ => {}
685        }
686    }
687
688    /// Waits out one tick, reporting whether the client is still running.
689    ///
690    /// Slept in short steps rather than one, so closing does not have to wait
691    /// out a whole tick before the thread can be joined.
692    fn sleep_until_next_tick(&self) -> bool {
693        let deadline = Instant::now() + PROBE_TICK;
694        while Instant::now() < deadline {
695            if self.closing.load(Ordering::SeqCst) {
696                return false;
697            }
698            std::thread::sleep(SHUTDOWN_POLL);
699        }
700        !self.closing.load(Ordering::SeqCst)
701    }
702
703    /// Whether it is time to announce again.
704    ///
705    /// This is a timer, not a response to traffic: a device announces itself on
706    /// a schedule whether or not anything is talking to it, and a client that
707    /// only re-announced when a datagram arrived would go silent on a quiet
708    /// network and stay unknown to every peer that started after it.
709    fn announce_due(&self, now: Instant) -> bool {
710        !self.closing.load(Ordering::SeqCst) && lock(&self.schedule).announce_due(now)
711    }
712
713    /// Reads datagrams until the client is closing.
714    fn receive_loop(&self) {
715        let mut buf = vec![0u8; RECV_BUFFER];
716        while !self.closing.load(Ordering::SeqCst) {
717            let Some(conn) = lock(&self.tx).conn() else {
718                break;
719            };
720            match conn.recv(&mut buf) {
721                Ok(Some((data, from))) => self.process_datagram(data, from),
722                Ok(None) => {}
723                Err(_) => break,
724            }
725        }
726    }
727
728    /// Runs [`Self::probe_once`] once a second until the client is closing.
729    fn probe_loop(&self) {
730        while self.sleep_until_next_tick() {
731            self.probe_once(Instant::now());
732        }
733    }
734
735    /// One pass: liveness, completion, the announcement timer and discovery.
736    pub(super) fn probe_once(&self, now: Instant) {
737        let want_discover = self.mutate(|state, ev| {
738            let mut incomplete = false;
739            let mut any_complete = false;
740            for device in state.devices.values_mut() {
741                device.check_online(now, ev);
742                // Re-tested here rather than only where a frame lands: a device
743                // stops waiting for its links on the clock alone, and no frame
744                // arrives to announce that.
745                device.check_config_complete(now, ev);
746                if device.configuration_complete(now) {
747                    any_complete = true;
748                } else if now.saturating_duration_since(device.first_seen) > CONFIG_GRACE {
749                    // Past the grace period a device has said nothing more, so
750                    // ask the network again rather than wait forever.
751                    incomplete = true;
752                }
753            }
754            // Nothing has finished describing itself, so nothing has been
755            // discovered yet at all.
756            incomplete || !any_complete
757        });
758
759        let discover_due = lock(&self.schedule).discover_due(now);
760        if self.announce_due(now) {
761            self.announce();
762        }
763        if want_discover && discover_due {
764            let _ = self.discover();
765        }
766    }
767}
768
769/// Takes a lock, continuing through a poisoning.
770///
771/// A poisoned lock here means a panic somewhere that held it. The state behind
772/// it is a cache of what devices have reported and is rebuilt by the next
773/// frame from each, so refusing to touch it again would retire a working
774/// client over one bad datagram.
775fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
776    m.lock().unwrap_or_else(|e| e.into_inner())
777}
778
779/// Loads the client's identifier, generating and storing one on first run.
780///
781/// A generated identifier that cannot be stored is still used: a client with a
782/// new identity each run is worse than one that works today, and the failure is
783/// the caller's home directory, not the network.
784fn load_uid(path: Option<PathBuf>) -> io::Result<DeviceUid> {
785    let path = path.or_else(|| {
786        std::env::var_os("HOME")
787            .or_else(|| std::env::var_os("USERPROFILE"))
788            .map(|home| PathBuf::from(home).join(UID_FILE))
789    });
790    if let Some(path) = &path {
791        if let Ok(bytes) = std::fs::read(path) {
792            if let Ok(array) = <[u8; 16]>::try_from(bytes.get(..16).unwrap_or_default()) {
793                return Ok(DeviceUid::from_array(array));
794            }
795        }
796    }
797    let mut bytes = [0u8; 16];
798    getrandom::getrandom(&mut bytes).map_err(|e| io::Error::other(e.to_string()))?;
799    if let Some(path) = &path {
800        let _ = std::fs::write(path, bytes);
801    }
802    Ok(DeviceUid::from_array(bytes))
803}
804
805/// The protocol floor is checked against what the device says it can decode.
806impl crate::wire::ProtocolTarget for Device {
807    fn serial(&self) -> &str {
808        Device::serial(self)
809    }
810
811    fn supported_protocol(&self) -> u16 {
812        self.hello.supported_protocol
813    }
814}