treadlers 0.1.0

A Rust library for controlling Treadly treadmills via Bluetooth Low Energy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use serde::{Deserialize, Serialize};
use std::{fmt, time::SystemTime};

/// Speed unit for treadmill operations
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SpeedUnit {
    /// Kilometers per hour
    Kilometers,
    /// Miles per hour
    Miles,
}

impl fmt::Display for SpeedUnit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Kilometers => write!(f, "km/h"),
            Self::Miles => write!(f, "mph"),
        }
    }
}

/// Device operational mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DeviceMode {
    /// Device is powered on but not active
    Awake = 0,
    /// Device is actively running
    Active = 1,
    /// Device is idle/paused
    Idle = 2,
    /// Unknown state
    Unknown = 3,
}

impl From<u8> for DeviceMode {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Awake,
            1 => Self::Active,
            2 => Self::Idle,
            _ => Self::Unknown,
        }
    }
}

impl fmt::Display for DeviceMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Awake => write!(f, "Awake"),
            Self::Active => write!(f, "Active"),
            Self::Idle => write!(f, "Idle"),
            Self::Unknown => write!(f, "Unknown"),
        }
    }
}

/// Emergency stop state
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EmergencyStopState {
    /// Normal operation
    Normal,
    /// Emergency stop is active
    Active,
    /// Emergency stop reset required
    ResetRequired,
}

impl fmt::Display for EmergencyStopState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Normal => write!(f, "Normal"),
            Self::Active => write!(f, "Emergency Stop Active"),
            Self::ResetRequired => write!(f, "Reset Required"),
        }
    }
}

/// Authentication status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuthenticationStatus {
    /// Not authenticated
    NotAuthenticated,
    /// Authentication in progress
    InProgress,
    /// Successfully authenticated
    Authenticated,
    /// Authentication failed
    Failed,
}

/// Temperature status codes extracted from decompiled Android app
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TemperatureStatus {
    /// Normal temperature operation
    Normal = 0,
    /// Critical temperature - forces emergency stop
    Stop = 1,
    /// High temperature - forces speed reduction
    ReduceSpeed = 2,
    /// Temperature sensor error
    Unknown = 3,
}

impl From<u8> for TemperatureStatus {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Normal,
            1 => Self::Stop,
            2 => Self::ReduceSpeed,
            _ => Self::Unknown,
        }
    }
}

impl fmt::Display for TemperatureStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Normal => write!(f, "Normal"),
            Self::Stop => write!(f, "Emergency Stop - Critical Temperature"),
            Self::ReduceSpeed => write!(f, "Warning - High Temperature"),
            Self::Unknown => write!(f, "Temperature Sensor Error"),
        }
    }
}

/// Device status codes extracted from decompiled Android app
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DeviceStatusCode {
    /// Normal operation
    NoError = 0,
    /// High temperature detected - safety condition
    HighTemperature = 1,
    /// `WiFi` scanning in progress
    WifiScanning = 2,
    /// Device requires power cycle - safety condition
    PowerCycleRequired = 3,
    /// Request processing error
    RequestError = 4,
    /// `WiFi` connection failed
    WifiNotConnected = 5,
    /// `WiFi` system error
    WifiError = 6,
}

impl From<u8> for DeviceStatusCode {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::NoError,
            1 => Self::HighTemperature,
            2 => Self::WifiScanning,
            3 => Self::PowerCycleRequired,
            5 => Self::WifiNotConnected,
            6 => Self::WifiError,
            _ => Self::RequestError,
        }
    }
}

impl fmt::Display for DeviceStatusCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoError => write!(f, "No Error"),
            Self::HighTemperature => write!(f, "High Temperature"),
            Self::WifiScanning => write!(f, "WiFi Scanning"),
            Self::PowerCycleRequired => write!(f, "Power Cycle Required"),
            Self::RequestError => write!(f, "Request Error"),
            Self::WifiNotConnected => write!(f, "WiFi Not Connected"),
            Self::WifiError => write!(f, "WiFi Error"),
        }
    }
}

/// Connection health status for monitoring connection safety
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConnectionHealth {
    /// Connection is healthy
    Healthy,
    /// Connection is degraded but functional
    Degraded,
    /// Connection is unstable
    Unstable,
    /// Connection is lost
    Lost,
}

impl fmt::Display for ConnectionHealth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Healthy => write!(f, "Healthy"),
            Self::Degraded => write!(f, "Degraded"),
            Self::Unstable => write!(f, "Unstable"),
            Self::Lost => write!(f, "Lost"),
        }
    }
}

/// Speed information
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct SpeedInfo {
    /// Current speed
    pub current: f32,
    /// Target speed
    pub target: f32,
    /// Minimum speed
    pub minimum: f32,
    /// Maximum speed
    pub maximum: f32,
    /// Speed unit
    pub unit: SpeedUnit,
}

impl SpeedInfo {
    /// Create new speed info
    #[must_use]
    pub const fn new(current: f32, target: f32, min: f32, max: f32, unit: SpeedUnit) -> Self {
        Self {
            current,
            target,
            minimum: min,
            maximum: max,
            unit,
        }
    }

    /// Convert speed to the specified unit
    #[must_use]
    pub fn convert_to(&self, unit: SpeedUnit) -> Self {
        if self.unit == unit {
            return *self;
        }

        let (current, target, min, max) = match (self.unit, unit) {
            (SpeedUnit::Kilometers, SpeedUnit::Miles) => (
                self.current * 0.6214,
                self.target * 0.6214,
                self.minimum * 0.6214,
                self.maximum * 0.6214,
            ),
            (SpeedUnit::Miles, SpeedUnit::Kilometers) => (
                self.current * 1.6093,
                self.target * 1.6093,
                self.minimum * 1.6093,
                self.maximum * 1.6093,
            ),
            _ => (self.current, self.target, self.minimum, self.maximum),
        };

        Self {
            current,
            target,
            minimum: min,
            maximum: max,
            unit,
        }
    }
}

/// Device status information
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DeviceStatus {
    /// Current speed information
    pub speed: SpeedInfo,
    /// Device operational mode
    pub mode: DeviceMode,
    /// Emergency stop state
    pub emergency_stop: EmergencyStopState,
    /// Power state
    pub power_on: bool,
    /// Handrail status
    pub handrail_enabled: bool,
    /// Current distance traveled
    pub distance: f32,
    /// Step count
    pub steps: u32,
    /// Device temperature (Celsius)
    pub temperature: f32,
    /// Temperature status for safety monitoring
    pub temperature_status: TemperatureStatus,
    /// Device status code for error monitoring
    pub device_status_code: DeviceStatusCode,
    /// Connection health for safety monitoring
    pub connection_health: ConnectionHealth,
    /// Authentication status
    pub authentication: AuthenticationStatus,
    /// Session active
    pub session_active: bool,
    /// Last status update timestamp
    pub timestamp: SystemTime,
}

impl Default for DeviceStatus {
    fn default() -> Self {
        Self {
            speed: SpeedInfo::new(0.0, 0.0, 0.0, 20.0, SpeedUnit::Kilometers),
            mode: DeviceMode::Unknown,
            emergency_stop: EmergencyStopState::Normal,
            power_on: false,
            handrail_enabled: true,
            distance: 0.0,
            steps: 0,
            temperature: 0.0,
            temperature_status: TemperatureStatus::Normal,
            device_status_code: DeviceStatusCode::NoError,
            connection_health: ConnectionHealth::Healthy,
            authentication: AuthenticationStatus::NotAuthenticated,
            session_active: false,
            timestamp: SystemTime::now(),
        }
    }
}

/// Device information
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeviceInfo {
    /// Device name
    pub name: String,
    /// Device MAC address
    pub mac_address: Option<String>,
    /// Signal strength (RSSI)
    pub rssi: i16,
    /// Device priority (from manufacturer data)
    pub priority: i8,
    /// Firmware version
    pub firmware_version: Option<String>,
    /// Hardware version
    pub hardware_version: Option<String>,
    /// Serial number
    pub serial_number: Option<String>,
}

impl DeviceInfo {
    /// Create new device info
    #[must_use]
    pub const fn new(name: String, rssi: i16, priority: i8) -> Self {
        Self {
            name,
            mac_address: None,
            rssi,
            priority,
            firmware_version: None,
            hardware_version: None,
            serial_number: None,
        }
    }
}

/// Connection parameters
#[derive(Debug, Clone)]
pub struct ConnectionParams {
    /// Connection timeout in milliseconds
    pub timeout_ms: u64,
    /// Enable authentication
    pub authenticate: bool,
    /// Retry attempts
    pub retry_attempts: u32,
    /// Scan timeout in milliseconds
    pub scan_timeout_ms: u64,
}

/// Protocol-compliant timeout configuration
#[derive(Debug, Clone)]
pub struct TimeoutConfig {
    /// Default command timeout in milliseconds
    pub default_timeout_ms: u64,
    /// Authentication timeout in milliseconds
    pub auth_timeout_ms: u64,
    /// Status request timeout in milliseconds
    pub status_timeout_ms: u64,
    /// Emergency stop timeout in milliseconds
    pub emergency_stop_timeout_ms: u64,
    /// Speed command timeout in milliseconds
    pub speed_command_timeout_ms: u64,
    /// Power command timeout in milliseconds
    pub power_command_timeout_ms: u64,
    /// Secure authentication timeout in milliseconds
    pub secure_auth_timeout_ms: u64,
    /// Connection health check timeout in milliseconds
    pub connection_health_timeout_ms: u64,
    /// Maximum retry attempts for failed commands
    pub max_retry_attempts: u32,
    /// Retry delay in milliseconds
    pub retry_delay_ms: u64,
}

impl Default for ConnectionParams {
    fn default() -> Self {
        Self {
            timeout_ms: 30_000,
            authenticate: true,
            retry_attempts: 3,
            scan_timeout_ms: 10_000,
        }
    }
}

impl Default for TimeoutConfig {
    fn default() -> Self {
        Self {
            default_timeout_ms: 3_000,
            auth_timeout_ms: 5_000,
            status_timeout_ms: 2_000,
            emergency_stop_timeout_ms: 5_000,
            speed_command_timeout_ms: 3_000,
            power_command_timeout_ms: 4_000,
            secure_auth_timeout_ms: 8_000,
            connection_health_timeout_ms: 30_000,
            max_retry_attempts: 3,
            retry_delay_ms: 500,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_speed_conversion() {
        let speed_km = SpeedInfo::new(10.0, 12.0, 0.0, 20.0, SpeedUnit::Kilometers);
        let speed_mph = speed_km.convert_to(SpeedUnit::Miles);

        assert!((speed_mph.current - 6.214).abs() < 0.01);
        assert_eq!(speed_mph.unit, SpeedUnit::Miles);
    }

    #[test]
    fn test_device_mode_from_u8() {
        assert_eq!(DeviceMode::from(0), DeviceMode::Awake);
        assert_eq!(DeviceMode::from(1), DeviceMode::Active);
        assert_eq!(DeviceMode::from(2), DeviceMode::Idle);
        assert_eq!(DeviceMode::from(99), DeviceMode::Unknown);
    }

    #[test]
    fn test_timeout_config_defaults() {
        let config = TimeoutConfig::default();

        assert_eq!(config.default_timeout_ms, 3_000);
        assert_eq!(config.auth_timeout_ms, 5_000);
        assert_eq!(config.status_timeout_ms, 2_000);
        assert_eq!(config.emergency_stop_timeout_ms, 5_000);
        assert_eq!(config.speed_command_timeout_ms, 3_000);
        assert_eq!(config.power_command_timeout_ms, 4_000);
        assert_eq!(config.secure_auth_timeout_ms, 8_000);
        assert_eq!(config.connection_health_timeout_ms, 30_000);
        assert_eq!(config.max_retry_attempts, 3);
        assert_eq!(config.retry_delay_ms, 500);
    }

    #[test]
    fn test_speed_unit_conversion() {
        let speed_km = SpeedInfo::new(10.0, 12.0, 0.0, 20.0, SpeedUnit::Kilometers);
        let speed_mph = speed_km.convert_to(SpeedUnit::Miles);

        // 10 km/h should be approximately 6.214 mph
        assert!((speed_mph.current - 6.214).abs() < 0.01);
        assert!((speed_mph.target - 7.457).abs() < 0.01);
        assert_eq!(speed_mph.unit, SpeedUnit::Miles);

        // Converting back should give original values
        let speed_km_back = speed_mph.convert_to(SpeedUnit::Kilometers);
        assert!((speed_km_back.current - 10.0).abs() < 0.01);
        assert!((speed_km_back.target - 12.0).abs() < 0.01);
    }

    #[test]
    fn test_device_info_creation() {
        let info = DeviceInfo::new("Test Treadmill".to_string(), -50, 1);
        assert_eq!(info.name, "Test Treadmill");
        assert_eq!(info.rssi, -50);
        assert_eq!(info.priority, 1);
        assert!(info.mac_address.is_none());
    }

    #[test]
    fn test_connection_params_default() {
        let params = ConnectionParams::default();
        assert_eq!(params.timeout_ms, 30_000);
        assert!(params.authenticate);
        assert_eq!(params.retry_attempts, 3);
        assert_eq!(params.scan_timeout_ms, 10_000);
    }
}