Skip to main content

strata_device/
mock.rs

1//! DeviceMockController: Thread-safe hardware simulation controller for tests, CI, and emulators.
2
3use crate::signals::{notify_memory_pressure, sync_signals_from_mock};
4use crate::types::*;
5use std::sync::{Arc, Mutex, OnceLock};
6
7/// Snapshot of simulated hardware telemetry and sensors.
8#[derive(Debug, Clone, PartialEq, Default)]
9pub struct DeviceTelemetrySnapshot {
10    pub battery: BatteryInfo,
11    pub thermal: ThermalState,
12    pub network: NetworkStatus,
13    pub screen: ScreenGeometry,
14    pub display_refresh: DisplayRefreshInfo,
15    pub gpu: GpuInfo,
16    pub geolocation: GeolocationInfo,
17    pub compass: CompassReading,
18    pub imu: ImuReading,
19    pub barometer: BarometerReading,
20    pub ambient_light: AmbientLightReading,
21}
22
23static GLOBAL_MOCK: OnceLock<Arc<Mutex<DeviceTelemetrySnapshot>>> = OnceLock::new();
24
25fn get_global_mock() -> &'static Arc<Mutex<DeviceTelemetrySnapshot>> {
26    GLOBAL_MOCK.get_or_init(|| Arc::new(Mutex::new(DeviceTelemetrySnapshot::default())))
27}
28
29/// Controller allowing programmatic mutation of simulated device telemetry and sensors.
30#[derive(Debug, Clone)]
31pub struct DeviceMockController {
32    state: Arc<Mutex<DeviceTelemetrySnapshot>>,
33}
34
35impl Default for DeviceMockController {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41impl DeviceMockController {
42    /// Obtains a controller handle connected to the global hardware telemetry simulation.
43    pub fn new() -> Self {
44        Self {
45            state: Arc::clone(get_global_mock()),
46        }
47    }
48
49    /// Resets all simulated device telemetry and sensors to standard factory defaults.
50    pub fn reset_to_defaults(&self) {
51        let mut guard = self.state.lock().unwrap();
52        *guard = DeviceTelemetrySnapshot::default();
53        drop(guard);
54        sync_signals_from_mock(self);
55    }
56
57    /// Gets a copy of current battery info.
58    pub fn battery(&self) -> BatteryInfo {
59        self.state.lock().unwrap().battery
60    }
61
62    /// Updates battery info and notifies reactive signals.
63    pub fn set_battery(&self, info: BatteryInfo) {
64        self.state.lock().unwrap().battery = info;
65        sync_signals_from_mock(self);
66    }
67
68    /// Updates battery level in range [0.0, 1.0].
69    pub fn set_battery_level(&self, level: f32) {
70        let mut guard = self.state.lock().unwrap();
71        guard.battery.level = level.clamp(0.0, 1.0);
72        drop(guard);
73        sync_signals_from_mock(self);
74    }
75
76    /// Gets current thermal state.
77    pub fn thermal(&self) -> ThermalState {
78        self.state.lock().unwrap().thermal
79    }
80
81    /// Updates thermal state and notifies reactive signals.
82    pub fn set_thermal(&self, state: ThermalState) {
83        self.state.lock().unwrap().thermal = state;
84        sync_signals_from_mock(self);
85    }
86
87    /// Gets current network status.
88    pub fn network(&self) -> NetworkStatus {
89        self.state.lock().unwrap().network
90    }
91
92    /// Updates network status and notifies reactive signals.
93    pub fn set_network(&self, status: NetworkStatus) {
94        self.state.lock().unwrap().network = status;
95        sync_signals_from_mock(self);
96    }
97
98    /// Sets online status.
99    pub fn set_online(&self, is_online: bool) {
100        let mut guard = self.state.lock().unwrap();
101        guard.network.is_online = is_online;
102        drop(guard);
103        sync_signals_from_mock(self);
104    }
105
106    /// Gets current screen geometry.
107    pub fn screen(&self) -> ScreenGeometry {
108        self.state.lock().unwrap().screen
109    }
110
111    /// Updates screen geometry and notifies reactive signals.
112    pub fn set_screen(&self, screen: ScreenGeometry) {
113        self.state.lock().unwrap().screen = screen;
114        sync_signals_from_mock(self);
115    }
116
117    /// Sets screen safe area insets (notches, dynamic island).
118    pub fn set_safe_area(&self, insets: SafeAreaInsets) {
119        let mut guard = self.state.lock().unwrap();
120        guard.screen.safe_area = insets;
121        drop(guard);
122        sync_signals_from_mock(self);
123    }
124
125    /// Gets current display refresh info.
126    pub fn display_refresh(&self) -> DisplayRefreshInfo {
127        self.state.lock().unwrap().display_refresh
128    }
129
130    /// Updates display refresh characteristics and capabilities.
131    pub fn set_display_refresh(&self, info: DisplayRefreshInfo) {
132        self.state.lock().unwrap().display_refresh = info;
133        sync_signals_from_mock(self);
134    }
135
136    /// Gets current GPU info.
137    pub fn gpu(&self) -> GpuInfo {
138        self.state.lock().unwrap().gpu.clone()
139    }
140
141    /// Updates GPU introspection info.
142    pub fn set_gpu(&self, gpu: GpuInfo) {
143        self.state.lock().unwrap().gpu = gpu;
144    }
145
146    /// Gets current geolocation telemetry snapshot.
147    pub fn geolocation(&self) -> GeolocationInfo {
148        self.state.lock().unwrap().geolocation.clone()
149    }
150
151    /// Sets full geolocation telemetry and notifies reactive `use_geolocation()` signals.
152    pub fn set_geolocation(&self, info: GeolocationInfo) {
153        self.state.lock().unwrap().geolocation = info;
154        sync_signals_from_mock(self);
155    }
156
157    /// Convenience helper injecting a deterministic `MockOverride` geolocation fix.
158    pub fn set_coordinates(
159        &self,
160        latitude: f64,
161        longitude: f64,
162        altitude_m: Option<f64>,
163        accuracy_m: f64,
164    ) {
165        let fix = GeolocationInfo::available(
166            latitude,
167            longitude,
168            accuracy_m,
169            GeolocationSource::MockOverride,
170        )
171        .with_altitude(altitude_m);
172        self.set_geolocation(fix);
173    }
174
175    /// Clears any simulated geolocation fix back to unavailable (`GeolocationSource::None`).
176    pub fn clear_geolocation(&self) {
177        self.set_geolocation(GeolocationInfo::default());
178    }
179
180    /// Gets current compass / magnetometer reading.
181    pub fn compass(&self) -> CompassReading {
182        self.state.lock().unwrap().compass
183    }
184
185    /// Updates compass / magnetometer reading and notifies reactive `use_compass()` signals.
186    pub fn set_compass(&self, reading: CompassReading) {
187        self.state.lock().unwrap().compass = reading;
188        sync_signals_from_mock(self);
189    }
190
191    /// Gets current 6-DoF IMU reading.
192    pub fn imu(&self) -> ImuReading {
193        self.state.lock().unwrap().imu
194    }
195
196    /// Updates 6-DoF IMU reading and notifies reactive `use_imu()` signals.
197    pub fn set_imu(&self, reading: ImuReading) {
198        self.state.lock().unwrap().imu = reading;
199        sync_signals_from_mock(self);
200    }
201
202    /// Gets current barometric pressure reading.
203    pub fn barometer(&self) -> BarometerReading {
204        self.state.lock().unwrap().barometer
205    }
206
207    /// Updates barometric pressure reading and notifies reactive `use_barometer()` signals.
208    pub fn set_barometer(&self, reading: BarometerReading) {
209        self.state.lock().unwrap().barometer = reading;
210        sync_signals_from_mock(self);
211    }
212
213    /// Gets current ambient light sensor reading.
214    pub fn ambient_light(&self) -> AmbientLightReading {
215        self.state.lock().unwrap().ambient_light
216    }
217
218    /// Updates ambient light sensor reading and notifies reactive `use_ambient_light()` signals.
219    pub fn set_ambient_light(&self, reading: AmbientLightReading) {
220        self.state.lock().unwrap().ambient_light = reading;
221        sync_signals_from_mock(self);
222    }
223
224    /// Triggers a system memory pressure notification across all registered callbacks.
225    pub fn trigger_memory_pressure(&self, level: MemoryPressure) {
226        notify_memory_pressure(level);
227    }
228}