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    pub proximity: ProximityReading,
22    pub hinge_angle: HingeAngleReading,
23    pub radio_signal: RadioSignalReading,
24    pub ambient_spl: AmbientSplReading,
25    pub pedometer: PedometerReading,
26    pub heart_rate: HeartRateReading,
27}
28
29static GLOBAL_MOCK: OnceLock<Arc<Mutex<DeviceTelemetrySnapshot>>> = OnceLock::new();
30
31fn get_global_mock() -> &'static Arc<Mutex<DeviceTelemetrySnapshot>> {
32    GLOBAL_MOCK.get_or_init(|| Arc::new(Mutex::new(DeviceTelemetrySnapshot::default())))
33}
34
35/// Controller allowing programmatic mutation of simulated device telemetry and sensors.
36#[derive(Debug, Clone)]
37pub struct DeviceMockController {
38    state: Arc<Mutex<DeviceTelemetrySnapshot>>,
39}
40
41impl Default for DeviceMockController {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl DeviceMockController {
48    /// Obtains a controller handle connected to the global hardware telemetry simulation.
49    pub fn new() -> Self {
50        Self {
51            state: Arc::clone(get_global_mock()),
52        }
53    }
54
55    /// Resets all simulated device telemetry and sensors to standard factory defaults.
56    pub fn reset_to_defaults(&self) {
57        let mut guard = self.state.lock().unwrap();
58        *guard = DeviceTelemetrySnapshot::default();
59        drop(guard);
60        sync_signals_from_mock(self);
61    }
62
63    /// Gets a copy of current battery info.
64    pub fn battery(&self) -> BatteryInfo {
65        self.state.lock().unwrap().battery
66    }
67
68    /// Updates battery info and notifies reactive signals.
69    pub fn set_battery(&self, info: BatteryInfo) {
70        self.state.lock().unwrap().battery = info;
71        sync_signals_from_mock(self);
72    }
73
74    /// Updates battery level in range [0.0, 1.0].
75    pub fn set_battery_level(&self, level: f32) {
76        let mut guard = self.state.lock().unwrap();
77        guard.battery.level = level.clamp(0.0, 1.0);
78        drop(guard);
79        sync_signals_from_mock(self);
80    }
81
82    /// Gets current thermal state.
83    pub fn thermal(&self) -> ThermalState {
84        self.state.lock().unwrap().thermal
85    }
86
87    /// Updates thermal state and notifies reactive signals.
88    pub fn set_thermal(&self, state: ThermalState) {
89        self.state.lock().unwrap().thermal = state;
90        sync_signals_from_mock(self);
91    }
92
93    /// Gets current network status.
94    pub fn network(&self) -> NetworkStatus {
95        self.state.lock().unwrap().network
96    }
97
98    /// Updates network status and notifies reactive signals.
99    pub fn set_network(&self, status: NetworkStatus) {
100        self.state.lock().unwrap().network = status;
101        sync_signals_from_mock(self);
102    }
103
104    /// Sets online status.
105    pub fn set_online(&self, is_online: bool) {
106        let mut guard = self.state.lock().unwrap();
107        guard.network.is_online = is_online;
108        drop(guard);
109        sync_signals_from_mock(self);
110    }
111
112    /// Gets current screen geometry.
113    pub fn screen(&self) -> ScreenGeometry {
114        self.state.lock().unwrap().screen
115    }
116
117    /// Updates screen geometry and notifies reactive signals.
118    pub fn set_screen(&self, screen: ScreenGeometry) {
119        self.state.lock().unwrap().screen = screen;
120        sync_signals_from_mock(self);
121    }
122
123    /// Sets screen safe area insets (notches, dynamic island).
124    pub fn set_safe_area(&self, insets: SafeAreaInsets) {
125        let mut guard = self.state.lock().unwrap();
126        guard.screen.safe_area = insets;
127        drop(guard);
128        sync_signals_from_mock(self);
129    }
130
131    /// Gets current display refresh info.
132    pub fn display_refresh(&self) -> DisplayRefreshInfo {
133        self.state.lock().unwrap().display_refresh
134    }
135
136    /// Updates display refresh characteristics and capabilities.
137    pub fn set_display_refresh(&self, info: DisplayRefreshInfo) {
138        self.state.lock().unwrap().display_refresh = info;
139        sync_signals_from_mock(self);
140    }
141
142    /// Gets current GPU info.
143    pub fn gpu(&self) -> GpuInfo {
144        self.state.lock().unwrap().gpu.clone()
145    }
146
147    /// Updates GPU introspection info.
148    pub fn set_gpu(&self, gpu: GpuInfo) {
149        self.state.lock().unwrap().gpu = gpu;
150    }
151
152    /// Gets current geolocation telemetry snapshot.
153    pub fn geolocation(&self) -> GeolocationInfo {
154        self.state.lock().unwrap().geolocation.clone()
155    }
156
157    /// Sets full geolocation telemetry and notifies reactive `use_geolocation()` signals.
158    pub fn set_geolocation(&self, info: GeolocationInfo) {
159        self.state.lock().unwrap().geolocation = info;
160        sync_signals_from_mock(self);
161    }
162
163    /// Convenience helper injecting a deterministic `MockOverride` geolocation fix.
164    pub fn set_coordinates(
165        &self,
166        latitude: f64,
167        longitude: f64,
168        altitude_m: Option<f64>,
169        accuracy_m: f64,
170    ) {
171        let fix = GeolocationInfo::available(
172            latitude,
173            longitude,
174            accuracy_m,
175            GeolocationSource::MockOverride,
176        )
177        .with_altitude(altitude_m);
178        self.set_geolocation(fix);
179    }
180
181    /// Clears any simulated geolocation fix back to unavailable (`GeolocationSource::None`).
182    pub fn clear_geolocation(&self) {
183        self.set_geolocation(GeolocationInfo::default());
184    }
185
186    /// Gets current compass / magnetometer reading.
187    pub fn compass(&self) -> CompassReading {
188        self.state.lock().unwrap().compass
189    }
190
191    /// Updates compass / magnetometer reading and notifies reactive `use_compass()` signals.
192    pub fn set_compass(&self, reading: CompassReading) {
193        self.state.lock().unwrap().compass = reading;
194        sync_signals_from_mock(self);
195    }
196
197    /// Gets current 6-DoF IMU reading.
198    pub fn imu(&self) -> ImuReading {
199        self.state.lock().unwrap().imu
200    }
201
202    /// Updates 6-DoF IMU reading and notifies reactive `use_imu()` signals.
203    pub fn set_imu(&self, reading: ImuReading) {
204        self.state.lock().unwrap().imu = reading;
205        sync_signals_from_mock(self);
206    }
207
208    /// Gets current barometric pressure reading.
209    pub fn barometer(&self) -> BarometerReading {
210        self.state.lock().unwrap().barometer
211    }
212
213    /// Updates barometric pressure reading and notifies reactive `use_barometer()` signals.
214    pub fn set_barometer(&self, reading: BarometerReading) {
215        self.state.lock().unwrap().barometer = reading;
216        sync_signals_from_mock(self);
217    }
218
219    /// Gets current ambient light sensor reading.
220    pub fn ambient_light(&self) -> AmbientLightReading {
221        self.state.lock().unwrap().ambient_light
222    }
223
224    /// Updates ambient light sensor reading and notifies reactive `use_ambient_light()` signals.
225    pub fn set_ambient_light(&self, reading: AmbientLightReading) {
226        self.state.lock().unwrap().ambient_light = reading;
227        sync_signals_from_mock(self);
228    }
229
230    pub fn proximity(&self) -> ProximityReading {
231        self.state.lock().unwrap().proximity
232    }
233
234    pub fn set_proximity(&self, reading: ProximityReading) {
235        self.state.lock().unwrap().proximity = reading;
236        sync_signals_from_mock(self);
237    }
238
239    pub fn hinge_angle(&self) -> HingeAngleReading {
240        self.state.lock().unwrap().hinge_angle
241    }
242
243    pub fn set_hinge_angle(&self, reading: HingeAngleReading) {
244        self.state.lock().unwrap().hinge_angle = reading;
245        sync_signals_from_mock(self);
246    }
247
248    pub fn radio_signal(&self) -> RadioSignalReading {
249        self.state.lock().unwrap().radio_signal.clone()
250    }
251
252    pub fn set_radio_signal(&self, reading: RadioSignalReading) {
253        self.state.lock().unwrap().radio_signal = reading;
254        sync_signals_from_mock(self);
255    }
256
257    pub fn ambient_spl(&self) -> AmbientSplReading {
258        self.state.lock().unwrap().ambient_spl
259    }
260
261    pub fn set_ambient_spl(&self, reading: AmbientSplReading) {
262        self.state.lock().unwrap().ambient_spl = reading;
263        sync_signals_from_mock(self);
264    }
265
266    pub fn pedometer(&self) -> PedometerReading {
267        self.state.lock().unwrap().pedometer
268    }
269
270    pub fn set_pedometer(&self, reading: PedometerReading) {
271        self.state.lock().unwrap().pedometer = reading;
272        sync_signals_from_mock(self);
273    }
274
275    pub fn heart_rate(&self) -> HeartRateReading {
276        self.state.lock().unwrap().heart_rate
277    }
278
279    pub fn set_heart_rate(&self, reading: HeartRateReading) {
280        self.state.lock().unwrap().heart_rate = reading;
281        sync_signals_from_mock(self);
282    }
283
284    /// Triggers a system memory pressure notification across all registered callbacks.
285    pub fn trigger_memory_pressure(&self, level: MemoryPressure) {
286        notify_memory_pressure(level);
287    }
288}