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 /// The overlay sync a management unit last sent, `None` until it has
399 /// sent one.
400 pub fn osd_sync(&self, uid: DeviceUid) -> Option<OsdSync> {
401 self.shared
402 .read(|state| state.device(uid)?.osd_sync.clone())
403 }
404
405 /// How a unit last reported getting on with an overlay sync, `None` until
406 /// it has reported. Match it to the sync by `seq` and the sync's source.
407 pub fn osd_sync_report(&self, uid: DeviceUid) -> Option<OsdSyncReport> {
408 self.shared
409 .read(|state| state.device(uid)?.osd_sync_report.clone())
410 }
411
412 /// The health and IP configuration a unit last reported, `None` until it
413 /// has reported them.
414 pub fn unit_status(&self, uid: DeviceUid) -> Option<UnitStatus> {
415 self.shared
416 .read(|state| state.device(uid)?.unit_status.clone())
417 }
418
419 /// The PTP state a V2IP device last reported about itself, `None` until
420 /// it has reported one, and while it has no PTP to report: none on its
421 /// video processor, or in power save.
422 pub fn v2ip_ptp(&self, uid: DeviceUid) -> Option<V2ipPtpState> {
423 self.shared.read(|state| state.device(uid)?.v2ip_ptp)
424 }
425
426 /// The test card a V2IP sink last reported, `None` until it has answered
427 /// [`Remote::request_v2ip_testcard`] or a change.
428 pub fn v2ip_testcard(&self, uid: DeviceUid) -> Option<V2ipTestcard> {
429 self.shared.read(|state| state.device(uid)?.v2ip_testcard)
430 }
431
432 /// The time zone a device announced for its mesh, `None` until it has.
433 ///
434 /// The mesh controller announces it with every periodic broadcast, empty
435 /// when it has none.
436 pub fn time_zone(&self, uid: DeviceUid) -> Option<TimeZone> {
437 self.shared
438 .read(|state| state.device(uid)?.time_zone.clone())
439 }
440
441 /// A device's clock as of now: the time it last announced, advanced by
442 /// how long ago that arrived. `None` until it has announced one.
443 ///
444 /// The mesh controller announces its clock with every periodic broadcast,
445 /// and only once it has been set.
446 pub fn device_clock(&self, uid: DeviceUid) -> Option<SystemTime> {
447 let (utc, at) = self.shared.read(|state| state.device(uid)?.clock)?;
448 let announced = UNIX_EPOCH.checked_add(Duration::from_secs(u64::from(utc)))?;
449 announced.checked_add(at.elapsed())
450 }
451
452 /// The video-wall tiling a V2IP device is configured for.
453 pub fn v2ip_tiling(&self, uid: DeviceUid) -> Option<V2ipTilingConfig> {
454 self.shared.read(|state| state.device(uid)?.tiling)
455 }
456
457 /// The audio endpoint tree a device exposes.
458 pub fn audio_endpoints(&self, uid: DeviceUid) -> Option<AudioEndpoints> {
459 self.shared.read(|state| state.device(uid)?.audio.clone())
460 }
461
462 /// The multiviewer layout a device is showing.
463 pub fn multiviewer_status(&self, uid: DeviceUid) -> Option<MultiviewerStatus> {
464 self.shared
465 .read(|state| state.device(uid)?.multiviewer.clone())
466 }
467
468 /// An amplifier's Dolby decoder settings.
469 pub fn dolby_settings(&self, uid: DeviceUid) -> Option<AmpDolbySettings> {
470 self.shared.read(|state| state.device(uid)?.dolby_settings)
471 }
472
473 /// A device's remote-control settings.
474 pub fn rc_settings(&self, uid: DeviceUid) -> Option<RcSettings> {
475 self.shared
476 .read(|state| state.device(uid)?.rc_settings.clone())
477 }
478
479 /// Every network port a device reports, in port order.
480 pub fn network_status(&self, uid: DeviceUid) -> Vec<NetworkPortStatus> {
481 self.shared.read(|state| {
482 state
483 .device(uid)
484 .map(|d| d.network.values().cloned().collect())
485 .unwrap_or_default()
486 })
487 }
488
489 /// The mesh topology a device reports.
490 pub fn topology(&self, uid: DeviceUid) -> Vec<TopologyEntry> {
491 self.shared.read(|state| {
492 state
493 .device(uid)
494 .map(|d| d.topology.clone())
495 .unwrap_or_default()
496 })
497 }
498
499 /// The EDID a device last reported: the display on its output, or the one
500 /// it presents to the source on its input.
501 ///
502 /// Filled in by a device's answer to [`Remote::request_edid`], and by any
503 /// answer to a peer's request that this client happened to hear.
504 pub fn edid(&self, uid: DeviceUid, output: bool) -> Option<Vec<u8>> {
505 self.shared
506 .read(|state| state.device(uid)?.edid(output).map(<[u8]>::to_vec))
507 }
508
509 /// How many frames from other senders have parsed since this client
510 /// started.
511 ///
512 /// It separates a mesh with nothing on it from an interface nothing is on:
513 /// a client that has discovered no device but is counting frames is
514 /// hearing traffic it cannot get answers from, which on a multi-homed host
515 /// is what a wrong [`Config::local_ip`] looks like. Frames this client
516 /// sent are not counted, because the host loops its own multicast back
517 /// whichever interface was selected.
518 pub fn frames_received(&self) -> u64 {
519 self.shared.read(|state| state.frames_received)
520 }
521
522 /// Every firmware image a device reports a version for.
523 pub fn firmware(&self, uid: DeviceUid) -> Vec<(FirmwareType, FirmwareVersion)> {
524 self.shared.read(|state| {
525 state
526 .device(uid)
527 .map(|d| d.firmware.iter().map(|(k, v)| (*k, v.clone())).collect())
528 .unwrap_or_default()
529 })
530 }
531
532 // ---- reconfiguring ----
533
534 /// Changes the interface and the multicast/broadcast mode while running,
535 /// reopening the socket when either differs from what is in use.
536 pub fn update_config(&self, local_ip: Option<Ipv4Addr>, broadcast: bool) -> io::Result<()> {
537 let network = {
538 let mut network = lock(&self.shared.network);
539 if network.local_ip == local_ip && network.broadcast == broadcast {
540 return Ok(());
541 }
542 network.local_ip = local_ip;
543 network.broadcast = broadcast;
544 network.clone()
545 };
546 // Opened before the old one is dropped, so a bind that fails leaves the
547 // client on the socket it had rather than on none.
548 let conn = network.open()?;
549 lock(&self.shared.tx).set_conn(Some(conn));
550 self.shared.announce();
551 let _ = self.shared.discover();
552 Ok(())
553 }
554
555 /// Asks every device on the network to announce itself.
556 pub fn discover(&self) -> Result<(), SendError> {
557 self.shared.discover()
558 }
559}
560
561impl Drop for Remote {
562 fn drop(&mut self) {
563 self.close();
564 }
565}
566
567impl Shared {
568 /// Reads the registry.
569 fn read<R>(&self, f: impl FnOnce(&State) -> R) -> R {
570 f(&lock(&self.state))
571 }
572
573 /// Mutates the registry, then delivers what changed.
574 ///
575 /// The queue is drained after the lock is released, so an event handler
576 /// may call back into the library.
577 fn mutate<R>(&self, f: impl FnOnce(&mut State, &mut Vec<Event>) -> R) -> R {
578 let mut events = Vec::new();
579 let result = {
580 let mut state = lock(&self.state);
581 let result = f(&mut state, &mut events);
582 state.mirror_mesh_events(&mut events);
583 result
584 };
585 self.dispatch(events);
586 result
587 }
588
589 fn dispatch(&self, events: Vec<Event>) {
590 for event in events {
591 event.dispatch(&*self.handler);
592 }
593 }
594
595 /// Decodes one datagram and delivers what it changed.
596 ///
597 /// This is the receive entry point. Keeping it distinct from the decode
598 /// below matters even though the wrapper is thin: announcing is driven by a
599 /// clock, and the one frame that may add a hello here is a ping addressed
600 /// to this client, which asks for exactly that.
601 fn process_datagram(&self, data: &[u8], from: Ipv4Addr) {
602 let (events, hello_requested, state_requests, osd_requests) = {
603 let mut state = lock(&self.state);
604 let events = process_frame(&mut state, data, Some(from), Instant::now());
605 (
606 events,
607 std::mem::take(&mut state.hello_requested),
608 std::mem::take(&mut state.state_requests),
609 std::mem::take(&mut state.osd_requests),
610 )
611 };
612 self.dispatch(events);
613 if hello_requested {
614 self.announce();
615 }
616 for device in state_requests {
617 self.request_state(device);
618 }
619 if !osd_requests.is_empty() {
620 self.answer_osd_requests(osd_requests);
621 }
622 }
623
624 /// Sends a frame, refusing one the addressee cannot decode.
625 fn send(&self, to: &Addressee, opcode: Opcode, payload: &[u8]) -> Result<usize, SendError> {
626 lock(&self.tx).send(to, self.uid, opcode, payload)
627 }
628
629 fn discover(&self) -> Result<(), SendError> {
630 lock(&self.schedule).discovered(Instant::now());
631 self.send(&Addressee::Broadcast, op::SYS_DISCOVER, &[])?;
632 Ok(())
633 }
634
635 /// Asks one device for everything it holds, with a discover naming it.
636 ///
637 /// Skipped while a discover to every device is recent enough to have
638 /// covered it. A receiver that predates the named form answers it as a
639 /// discover to every device, so each one asked costs the whole mesh's
640 /// state on such a network.
641 fn request_state(&self, device: DeviceUid) {
642 if lock(&self.schedule).discover_covers(Instant::now()) {
643 return;
644 }
645 let _ = self.send(&Addressee::Broadcast, op::SYS_DISCOVER, device.as_bytes());
646 }
647
648 /// Announces this client, and re-arms the announcement timer only once the
649 /// frame is away.
650 ///
651 /// The firmware resets its own hello timeout inside the branch where the
652 /// transmit succeeded. A send that fails is then retried on the next tick
653 /// rather than costing a whole interval of silence, which matters most at
654 /// startup and after a network blip: exactly when being heard is worth the
655 /// most.
656 ///
657 /// It announces `CONFIG_INITIALISED` because a unit uses the scaling
658 /// fields of a 0x3C write only from a sender that does, and this client
659 /// zeroes every field it does not set.
660 ///
661 /// While it holds an overlay sync it also announces itself as the mesh's
662 /// overlay sync manager, which is what has the controller leave units'
663 /// requests for a new sync to it.
664 fn announce(&self) {
665 let mut features = DeviceFeature::MANAGER | DeviceFeature::CONFIG_INITIALISED;
666 if lock(&self.osd).holds() {
667 features |= DeviceFeature::OSD_SYNC;
668 }
669 let payload = build_hello(
670 PROTOCOL_VERSION,
671 &self.name,
672 CLIENT_SERIAL,
673 VERSION,
674 features.bits(),
675 );
676 match self.send(&Addressee::Broadcast, op::SYS_HELLO, &payload) {
677 Ok(n) if n > 0 => lock(&self.schedule).announced(Instant::now()),
678 _ => {}
679 }
680 }
681
682 /// Waits out one tick, reporting whether the client is still running.
683 ///
684 /// Slept in short steps rather than one, so closing does not have to wait
685 /// out a whole tick before the thread can be joined.
686 fn sleep_until_next_tick(&self) -> bool {
687 let deadline = Instant::now() + PROBE_TICK;
688 while Instant::now() < deadline {
689 if self.closing.load(Ordering::SeqCst) {
690 return false;
691 }
692 std::thread::sleep(SHUTDOWN_POLL);
693 }
694 !self.closing.load(Ordering::SeqCst)
695 }
696
697 /// Whether it is time to announce again.
698 ///
699 /// This is a timer, not a response to traffic: a device announces itself on
700 /// a schedule whether or not anything is talking to it, and a client that
701 /// only re-announced when a datagram arrived would go silent on a quiet
702 /// network and stay unknown to every peer that started after it.
703 fn announce_due(&self, now: Instant) -> bool {
704 !self.closing.load(Ordering::SeqCst) && lock(&self.schedule).announce_due(now)
705 }
706
707 /// Reads datagrams until the client is closing.
708 fn receive_loop(&self) {
709 let mut buf = vec![0u8; RECV_BUFFER];
710 while !self.closing.load(Ordering::SeqCst) {
711 let Some(conn) = lock(&self.tx).conn() else {
712 break;
713 };
714 match conn.recv(&mut buf) {
715 Ok(Some((data, from))) => self.process_datagram(data, from),
716 Ok(None) => {}
717 Err(_) => break,
718 }
719 }
720 }
721
722 /// Runs [`Self::probe_once`] once a second until the client is closing.
723 fn probe_loop(&self) {
724 while self.sleep_until_next_tick() {
725 self.probe_once(Instant::now());
726 }
727 }
728
729 /// One pass: liveness, completion, the announcement timer and discovery.
730 pub(super) fn probe_once(&self, now: Instant) {
731 let want_discover = self.mutate(|state, ev| {
732 let mut incomplete = false;
733 let mut any_complete = false;
734 for device in state.devices.values_mut() {
735 device.check_online(now, ev);
736 // Re-tested here rather than only where a frame lands: a device
737 // stops waiting for its links on the clock alone, and no frame
738 // arrives to announce that.
739 device.check_config_complete(now, ev);
740 if device.configuration_complete(now) {
741 any_complete = true;
742 } else if now.saturating_duration_since(device.first_seen) > CONFIG_GRACE {
743 // Past the grace period a device has said nothing more, so
744 // ask the network again rather than wait forever.
745 incomplete = true;
746 }
747 }
748 // Nothing has finished describing itself, so nothing has been
749 // discovered yet at all.
750 incomplete || !any_complete
751 });
752
753 let discover_due = lock(&self.schedule).discover_due(now);
754 if self.announce_due(now) {
755 self.announce();
756 }
757 if want_discover && discover_due {
758 let _ = self.discover();
759 }
760 }
761}
762
763/// Takes a lock, continuing through a poisoning.
764///
765/// A poisoned lock here means a panic somewhere that held it. The state behind
766/// it is a cache of what devices have reported and is rebuilt by the next
767/// frame from each, so refusing to touch it again would retire a working
768/// client over one bad datagram.
769fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
770 m.lock().unwrap_or_else(|e| e.into_inner())
771}
772
773/// Loads the client's identifier, generating and storing one on first run.
774///
775/// A generated identifier that cannot be stored is still used: a client with a
776/// new identity each run is worse than one that works today, and the failure is
777/// the caller's home directory, not the network.
778fn load_uid(path: Option<PathBuf>) -> io::Result<DeviceUid> {
779 let path = path.or_else(|| {
780 std::env::var_os("HOME")
781 .or_else(|| std::env::var_os("USERPROFILE"))
782 .map(|home| PathBuf::from(home).join(UID_FILE))
783 });
784 if let Some(path) = &path {
785 if let Ok(bytes) = std::fs::read(path) {
786 if let Ok(array) = <[u8; 16]>::try_from(bytes.get(..16).unwrap_or_default()) {
787 return Ok(DeviceUid::from_array(array));
788 }
789 }
790 }
791 let mut bytes = [0u8; 16];
792 getrandom::getrandom(&mut bytes).map_err(|e| io::Error::other(e.to_string()))?;
793 if let Some(path) = &path {
794 let _ = std::fs::write(path, bytes);
795 }
796 Ok(DeviceUid::from_array(bytes))
797}
798
799/// The protocol floor is checked against what the device says it can decode.
800impl crate::wire::ProtocolTarget for Device {
801 fn serial(&self) -> &str {
802 Device::serial(self)
803 }
804
805 fn supported_protocol(&self) -> u16 {
806 self.hello.supported_protocol
807 }
808}