1use crate::types::*;
7use crate::wire::{
8 BayUid, DeviceUid, EdidProfile, LinkFeature, RcAction, RcKey, RcType, V2ipFpgaFeature,
9};
10
11macro_rules! events {
24 (
25 device {
26 $( $(#[$dmeta:meta])* $dvariant:ident => $dmethod:ident ( $($darg:ident : $dty:ty),* ); )*
27 }
28 bay {
29 $( $(#[$bmeta:meta])* $bvariant:ident => $bmethod:ident ( $($barg:ident : $bty:ty),* ); )*
30 }
31 bay_and_device {
32 $( $(#[$lmeta:meta])* $lvariant:ident => $lmethod:ident ( $($larg:ident : $lty:ty),* ); )*
33 }
34 ) => {
35 #[derive(Clone, Debug, PartialEq)]
40 #[non_exhaustive]
41 pub enum Event {
42 $(
43 $(#[$dmeta])*
44 $dvariant {
45 device: DeviceUid,
47 $( #[allow(missing_docs)] $darg: $dty, )*
48 },
49 )*
50 $(
51 $(#[$bmeta])*
52 $bvariant {
53 bay: BayUid,
55 $( #[allow(missing_docs)] $barg: $bty, )*
56 },
57 )*
58 $(
59 $(#[$lmeta])*
60 $lvariant {
61 bay: BayUid,
63 $( #[allow(missing_docs)] $larg: $lty, )*
64 },
65 )*
66 }
67
68 #[allow(unused_variables)]
76 pub trait EventHandler: Send + Sync {
77 $(
78 $(#[$dmeta])*
79 fn $dmethod(&self, device: DeviceUid $(, $darg: $dty)*) {}
80 )*
81 $(
82 $(#[$bmeta])*
83 fn $bmethod(&self, bay: BayUid $(, $barg: $bty)*) {}
84 )*
85 $(
86 $(#[$lmeta])*
87 fn $lmethod(&self, bay: BayUid $(, $larg: $lty)*) {}
88 )*
89
90 fn on_device_update(&self, device: DeviceUid) {}
92
93 fn on_bay_update(&self, bay: BayUid) {}
95 }
96
97 impl Event {
98 pub(crate) fn dispatch(self, handler: &dyn EventHandler) {
101 match self {
102 $(
103 Self::$dvariant { device $(, $darg)* } => {
104 handler.$dmethod(device $(, $darg)*);
105 handler.on_device_update(device);
106 }
107 )*
108 $(
109 Self::$bvariant { bay $(, $barg)* } => {
110 handler.$bmethod(bay $(, $barg)*);
111 handler.on_bay_update(bay);
112 }
113 )*
114 $(
115 Self::$lvariant { bay $(, $larg)* } => {
116 handler.$lmethod(bay $(, $larg)*);
117 handler.on_bay_update(bay);
118 handler.on_device_update(bay.device);
119 }
120 )*
121 }
122 }
123 }
124 };
125}
126
127impl EventHandler for () {}
134
135events! {
136 device {
137 DeviceConfigChanged => on_device_config_changed();
139 DeviceConfigComplete => on_device_config_complete();
141 DeviceOnlineChanged => on_device_online_changed(online: bool);
143 DeviceTemperatureChanged => on_device_temperature_changed(temperatures: Vec<u8>);
145 FirmwareVersionChanged => on_firmware_version_changed(version: FirmwareVersion);
147 SystemStatusChanged => on_system_status_changed(status: u16, message: String);
149 NetworkStatusChanged => on_network_status_changed(status: NetworkPortStatus);
151 V2ipStatsChanged => on_v2ip_stats_changed(stats: V2ipDeviceStats);
153 V2ipSourcesChanged => on_v2ip_sources_changed(sources: Vec<V2ipStreamSources>);
155 V2ipDetailsChanged => on_v2ip_details_changed(details: DeviceV2ipDetails);
157 V2ipSinkChanged => on_v2ip_sink_changed(sink: DeviceV2ipSink);
160 MultiviewerStatusChanged => on_multiviewer_status_changed(status: MultiviewerStatus);
162 AudioEndpointsChanged => on_audio_endpoints_changed(endpoints: AudioEndpoints);
165 MeshMasterChanged => on_mesh_master_changed(master: DeviceUid);
167 TopologyChanged => on_topology_changed(topology: Vec<TopologyEntry>);
169 AmpDolbySettingsChanged => on_amp_dolby_settings_changed(settings: AmpDolbySettings);
171 SetupStatusChanged => on_setup_status_changed(completed: bool);
173 InstallerIdChanged => on_installer_id_changed(installer_id: u16);
175 TilingChanged => on_tiling_changed(tiling: V2ipTilingConfig);
177 V2ipFeaturesChanged => on_v2ip_features_changed(features: V2ipFpgaFeature);
179 V2ipDeviceSettingsChanged => on_v2ip_device_settings_changed(settings: V2ipDeviceSettings);
181 V2ipVlanChanged => on_v2ip_vlan_changed(vlan: V2ipVlan);
183 UnitStatusChanged => on_unit_status_changed(status: UnitStatus);
185 V2ipPtpChanged => on_v2ip_ptp_changed(ptp: Option<V2ipPtpState>);
188 V2ipTestcardChanged => on_v2ip_testcard_changed(testcard: V2ipTestcard);
190 TimeZoneChanged => on_time_zone_changed(time_zone: TimeZone);
192 RcSettingsChanged => on_rc_settings_changed(settings: RcSettings);
194 V2ipLinkChanged => on_v2ip_link_changed(target: DeviceUid);
196 MultiviewerCommand => on_multiviewer_command(command: MultiviewerCommand);
198 AudioSelectInput => on_audio_select_input(change: AudioChangeSource);
200 AudioEndpointMute => on_audio_endpoint_mute(endpoint: u16, muted: bool);
202 AudioEndpointTrigger => on_audio_endpoint_trigger(endpoint: u16, active: bool);
204 AudioEndpointLock => on_audio_endpoint_lock(endpoint: u16, locked: bool);
206 AudioEndpointVolume => on_audio_endpoint_volume(endpoint: u16, volume: u32);
208 DiscoverRequest => on_discover_request();
210 SetRouteRequested => on_set_route_requested(request: SetRouteRequest);
212 EdidRequested => on_edid_requested(request: EdidRequest);
214 EdidReceived => on_edid_received(edid: EdidRecord);
216 BayNameChangeRequested => on_bay_name_change_requested(change: BayNameChange);
218 EdidProfileChangeRequested => on_edid_profile_change_requested(change: EdidProfileChange);
220 RebootRequested => on_reboot_requested(request: RebootRequest);
222 UnitCommandRequested => on_unit_command_requested(command: UnitCommand);
224 FactoryResetRequested => on_factory_reset_requested(request: FactoryResetRequest);
226 MonitoringPulse => on_monitoring_pulse();
228 UpgradeFpgaRequested => on_upgrade_fpga_requested();
230 DetectBaysRequested => on_detect_bays_requested();
232 PowerSaveRequested => on_power_save_requested(request: V2ipPowerSaveRequest);
234 KeyTransmitRequested => on_key_transmit_requested(request: KeyTransmitRequest);
236 ActionTransmitRequested => on_action_transmit_requested(request: ActionTransmitRequest);
238 IrTransmitRequested => on_ir_transmit_requested(request: IrTransmitRequest);
240 BlacklistChanged => on_blacklist_changed(change: V2ipBlacklistChange);
242 VideoWallCommand => on_video_wall_command(command: VideoWallCommand);
244 }
245 bay {
246 BayRegistered => on_bay_registered();
248 VideoSourceChanged => on_video_source_changed(source: Option<BayUid>);
250 AudioSourceChanged => on_audio_source_changed(source: Option<BayUid>);
252 VolumeChanged => on_volume_changed(volume: VolumeMuteStatus);
254 PowerChanged => on_power_changed(power: PowerStatus);
256 NameChanged => on_name_changed(name: String);
258 SignalDetectedChanged => on_signal_detected_changed(detected: bool);
260 FaultyChanged => on_faulty_changed(faulty: bool);
262 HiddenChanged => on_hidden_changed(hidden: bool);
264 PoePoweredChanged => on_poe_powered_changed(powered: bool);
266 HdbtConnectedChanged => on_hdbt_connected_changed(connected: bool);
268 SignalTypeChanged => on_signal_type_changed(signal_type: String);
270 HpdDetectedChanged => on_hpd_detected_changed(detected: bool);
272 CecDetectedChanged => on_cec_detected_changed(detected: bool);
274 ArcChanged => on_arc_changed(arc: ArcStatus);
276 EdidProfileChanged => on_edid_profile_changed(profile: EdidProfile);
278 RcTypeChanged => on_rc_type_changed(rc_type: RcType);
280 KeyPressed => on_key_pressed(key: RcKey);
282 ActionReceived => on_action_received(action: RcAction);
284 MirrorStatusChanged => on_mirror_status_changed(mirror: BayMirrorStatus);
286 AmpZoneSettingsChanged => on_amp_zone_settings_changed(settings: AmpZoneSettings);
288 VolumeStep => on_volume_step(up: bool);
290 AudioClipped => on_audio_clip(clip: AudioClip);
292 IrCaptured => on_ir_captured(capture: IrCapture);
294 FilteredDevicesChanged => on_filtered_devices_changed(filtered: Vec<DeviceUid>);
296 AudioEndpointChanged => on_audio_endpoint_changed(endpoint: u8);
298 EncoderDisabledChanged => on_encoder_disabled_changed(disabled: bool);
300 DecoderDisabledChanged => on_decoder_disabled_changed(disabled: bool);
302 }
303 bay_and_device {
304 BayLinked => on_bay_linked(linked_serial: String, bay_name: String, features: LinkFeature);
311 BayUnlinked => on_bay_unlinked(linked_serial: String, bay_name: String);
316 }
317}
318
319#[cfg(test)]
320mod tests {
321 use super::*;
322 use std::sync::Mutex;
323
324 #[derive(Default)]
325 struct Recorder {
326 calls: Mutex<Vec<String>>,
327 }
328
329 impl Recorder {
330 fn record(&self, what: &str) {
331 if let Ok(mut calls) = self.calls.lock() {
332 calls.push(what.to_owned());
333 }
334 }
335
336 fn calls(&self) -> Vec<String> {
337 self.calls.lock().map(|c| c.clone()).unwrap_or_default()
338 }
339 }
340
341 impl EventHandler for Recorder {
342 fn on_power_changed(&self, _bay: BayUid, power: PowerStatus) {
343 self.record(&format!("power={power}"));
344 }
345
346 fn on_setup_status_changed(&self, _device: DeviceUid, completed: bool) {
347 self.record(&format!("setup={completed}"));
348 }
349
350 fn on_bay_update(&self, _bay: BayUid) {
351 self.record("bay_update");
352 }
353
354 fn on_device_update(&self, _device: DeviceUid) {
355 self.record("device_update");
356 }
357 }
358
359 const DEVICE: DeviceUid = DeviceUid::from_array([9; 16]);
360
361 #[test]
362 fn a_bay_event_fires_its_own_method_then_the_generic_bay_update() {
363 let recorder = Recorder::default();
364 Event::PowerChanged {
365 bay: BayUid::new(DEVICE, 3),
366 power: PowerStatus::On,
367 }
368 .dispatch(&recorder);
369 assert_eq!(recorder.calls(), ["power=on", "bay_update"]);
370 }
371
372 #[test]
373 fn a_device_event_fires_its_own_method_then_the_generic_device_update() {
374 let recorder = Recorder::default();
375 Event::SetupStatusChanged {
376 device: DEVICE,
377 completed: true,
378 }
379 .dispatch(&recorder);
380 assert_eq!(recorder.calls(), ["setup=true", "device_update"]);
381 }
382
383 #[test]
384 fn a_link_event_fires_both_generic_updates() {
385 let recorder = Recorder::default();
386 Event::BayUnlinked {
387 bay: BayUid::new(DEVICE, 3),
388 linked_serial: "AB1234".to_owned(),
389 bay_name: "Output 1".to_owned(),
390 }
391 .dispatch(&recorder);
392 assert_eq!(recorder.calls(), ["bay_update", "device_update"]);
393 }
394
395 #[test]
396 fn an_event_a_handler_does_not_name_still_fires_the_generic_update() {
397 let recorder = Recorder::default();
398 Event::MonitoringPulse { device: DEVICE }.dispatch(&recorder);
399 assert_eq!(
400 recorder.calls(),
401 ["device_update"],
402 "the default no-op must not swallow the fan-in"
403 );
404 }
405}