dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//!     http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

//! # Platform Detection Module
//!
//! This module provides cross-platform hardware detection capabilities.
//! It automatically detects the operating system and platform characteristics
//! to enable appropriate discovery strategies for any hardware type.
//!
//! ## Supported Platforms
//!
//! - **Linux**: Full support including sysfs, procfs, D-Bus, udev
//! - **macOS**: Full support using IOKit and system calls
//! - **Windows**: Full support using WMI and system APIs
//! - **WebAssembly**: Limited support with virtual devices
//!
//! ## Usage
//!
//! ```rust,ignore
//! use ri::device::discovery::platform::{PlatformInfo, PlatformType, get_platform_info};
//!
//! let platform = get_platform_info();
//! println!("Platform: {:?}", platform.platform_type);
//! println!("OS: {}", platform.os_name);
//! println!("Architecture: {}", platform.arch);
//! ```

use std::process::Command;
use serde::{Serialize, Deserialize};

/// Supported platform types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub enum PlatformType {
    /// Linux-based operating systems
    Linux,
    /// macOS (Darwin)
    MacOS,
    /// Microsoft Windows
    Windows,
    /// WebAssembly environment
    WebAssembly,
    /// Unknown or unsupported platform
    Unknown,
}

/// CPU architecture types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub enum Architecture {
    /// x86_64 (AMD64)
    X86_64,
    /// AArch64 (ARM64)
    AArch64,
    /// x86 (i386/i686)
    X86,
    /// ARM (32-bit)
    Arm,
    /// WebAssembly
    Wasm,
    /// Unknown architecture
    Unknown,
}

/// Platform information container
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct PlatformInfo {
    /// Detected platform type
    pub platform_type: PlatformType,
    /// Operating system name
    pub os_name: String,
    /// Operating system version
    pub os_version: String,
    /// CPU architecture
    pub architecture: Architecture,
    /// Number of available CPU cores
    pub cpu_cores: usize,
    /// Amount of available memory in bytes
    pub total_memory: u64,
    /// Whether running in a container
    pub in_container: bool,
    /// Whether running in a virtual machine
    pub in_vm: bool,
    /// Available discovery strategies for this platform
    pub available_strategies: Vec<DiscoveryStrategy>,
}

/// Discovery strategies available on the current platform
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub enum DiscoveryStrategy {
    /// Read from /proc filesystem (Linux)
    ProcFs,
    /// Read from /sys filesystem (Linux)
    SysFs,
    /// Use D-Bus for device enumeration (Linux)
    DBus,
    /// Use udev for device enumeration (Linux)
    UDev,
    /// Use IOKit (macOS)
    IOKit,
    /// Use WMI (Windows)
    WMI,
    /// Use Windows Registry
    Registry,
    /// Use system calls and APIs
    SystemCalls,
    /// Use network scanning
    NetworkScan,
    /// Use virtual filesystem
    VirtualFS,
    /// Fallback to mock devices
    MockFallback,
}

impl PlatformInfo {
    /// Creates a new platform info instance with detected values
    pub fn new() -> Self {
        let platform_info = detect_platform();
        platform_info
    }

    /// Checks if a specific discovery strategy is available
    pub fn has_strategy(&self, strategy: &DiscoveryStrategy) -> bool {
        self.available_strategies.contains(strategy)
    }

    /// Returns the best available discovery strategy for hardware
    pub fn best_hardware_strategy(&self) -> DiscoveryStrategy {
        match self.platform_type {
            PlatformType::Linux => {
                if self.has_strategy(&DiscoveryStrategy::SysFs) {
                    return DiscoveryStrategy::SysFs;
                }
                if self.has_strategy(&DiscoveryStrategy::ProcFs) {
                    return DiscoveryStrategy::ProcFs;
                }
                if self.has_strategy(&DiscoveryStrategy::UDev) {
                    return DiscoveryStrategy::UDev;
                }
                DiscoveryStrategy::MockFallback
            }
            PlatformType::MacOS => {
                if self.has_strategy(&DiscoveryStrategy::IOKit) {
                    return DiscoveryStrategy::IOKit;
                }
                DiscoveryStrategy::SystemCalls
            }
            PlatformType::Windows => {
                if self.has_strategy(&DiscoveryStrategy::WMI) {
                    return DiscoveryStrategy::WMI;
                }
                DiscoveryStrategy::SystemCalls
            }
            PlatformType::WebAssembly => DiscoveryStrategy::VirtualFS,
            PlatformType::Unknown => DiscoveryStrategy::MockFallback,
        }
    }

    /// Returns the best available discovery strategy for network devices
    pub fn best_network_strategy(&self) -> DiscoveryStrategy {
        match self.platform_type {
            PlatformType::Linux => {
                if self.has_strategy(&DiscoveryStrategy::NetworkScan) {
                    return DiscoveryStrategy::NetworkScan;
                }
                DiscoveryStrategy::SysFs
            }
            PlatformType::MacOS => DiscoveryStrategy::SystemCalls,
            PlatformType::Windows => DiscoveryStrategy::WMI,
            PlatformType::WebAssembly => DiscoveryStrategy::VirtualFS,
            PlatformType::Unknown => DiscoveryStrategy::MockFallback,
        }
    }
}

impl Default for PlatformInfo {
    fn default() -> Self {
        Self::new()
    }
}

/// Detects the current platform and returns platform information
pub fn get_platform_info() -> PlatformInfo {
    detect_platform()
}

/// Detects the current platform
fn detect_platform() -> PlatformInfo {
    let (os_name, os_version) = detect_os();
    let architecture = detect_architecture();
    let cpu_cores = detect_cpu_cores();
    let total_memory = detect_total_memory();
    let in_container = detect_container();
    let in_vm = detect_virtual_machine();
    let platform_type = detect_platform_type(&os_name);
    let available_strategies = detect_available_strategies(&platform_type);

    PlatformInfo {
        platform_type,
        os_name,
        os_version,
        architecture,
        cpu_cores,
        total_memory,
        in_container,
        in_vm,
        available_strategies,
    }
}

/// Detects the operating system name and version
fn detect_os() -> (String, String) {
    let os = std::env::consts::OS;
    let version = std::env::consts::FAMILY;

    match os {
        "linux" => {
            let version_info = read_os_release();
            let name = version_info.get("NAME").cloned().unwrap_or_else(|| "Linux".to_string());
            let version_id = version_info.get("VERSION_ID")
                .or(version_info.get("VERSION"))
                .cloned()
                .unwrap_or_else(|| "unknown".to_string());
            (name, version_id)
        }
        "macos" => {
            let version = detect_macos_version();
            ("macOS".to_string(), version)
        }
        "windows" => {
            let version = detect_windows_version();
            ("Windows".to_string(), version)
        }
        _ => (os.to_string(), version.to_string()),
    }
}

/// Reads /etc/os-release for Linux distribution info
fn read_os_release() -> FxHashMap<String, String> {
    let mut result = FxHashMap::default();

    if let Ok(content) = std::fs::read_to_string("/etc/os-release") {
        for line in content.lines() {
            if let Some(eq_pos) = line.find('=') {
                let key = line[..eq_pos].to_string();
                let value = line[eq_pos + 1..].trim_matches('"').to_string();
                result.insert(key, value);
            }
        }
    }

    result
}

/// Detects macOS version
fn detect_macos_version() -> String {
    let output = Command::new("sw_vers")
        .arg("-productVersion")
        .output();

    match output {
        Ok(output) => {
            if output.status.success() {
                if let Ok(version) = String::from_utf8(output.stdout) {
                    return version.trim().to_string();
                }
            }
        }
        Err(_) => {}
    }

    "unknown".to_string()
}

/// Detects Windows version
fn detect_windows_version() -> String {
    let output = Command::new("cmd")
        .args(&["/c", "ver"])
        .output();

    match output {
        Ok(output) => {
            if output.status.success() {
                if let Ok(version) = String::from_utf8(output.stdout) {
                    return version.trim().to_string();
                }
            }
        }
        Err(_) => {}
    }

    "unknown".to_string()
}

/// Detects the CPU architecture
fn detect_architecture() -> Architecture {
    let arch = std::env::consts::ARCH;

    match arch {
        "x86_64" => Architecture::X86_64,
        "aarch64" | "arm64" => Architecture::AArch64,
        "x86" | "i686" | "i386" => Architecture::X86,
        "arm" | "thumbv7" => Architecture::Arm,
        "wasm32" => Architecture::Wasm,
        _ => Architecture::Unknown,
    }
}

/// Detects the number of CPU cores
fn detect_cpu_cores() -> usize {
    std::thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(1)
}

/// Detects total system memory
fn detect_total_memory() -> u64 {
    if let Ok(content) = std::fs::read_to_string("/proc/meminfo") {
        for line in content.lines() {
            if line.starts_with("MemTotal:") {
                if let Some(kb_str) = line.split_whitespace().nth(1) {
                    if let Ok(kb) = kb_str.parse::<u64>() {
                        return kb * 1024;
                    }
                }
            }
        }
    }
    4 * 1024 * 1024 * 1024 // Default to 4GB if detection fails
}

/// Detects if running inside a container
fn detect_container() -> bool {
    // Check for container-specific files and environment variables

    // Docker
    if std::env::var("DOCKER_CONTAINER").is_ok() {
        return true;
    }

    // Check for .dockerenv file
    if std::path::Path::new("/.dockerenv").exists() {
        return true;
    }

    // Check for container cgroup
    if let Ok(content) = std::fs::read_to_string("/proc/1/cgroup") {
        if content.contains("docker") || content.contains("containerd") {
            return true;
        }
    }

    // Check for k8s
    if std::env::var("KUBERNETES_SERVICE_HOST").is_ok() {
        return true;
    }

    false
}

/// Detects if running inside a virtual machine
fn detect_virtual_machine() -> bool {
    // Check for hypervisor
    if let Ok(content) = std::fs::read_to_string("/sys/hypervisor/type") {
        if content.trim() == "xen" || content.contains("vmware") || content.contains("kvm") {
            return true;
        }
    }

    // Check for VMware
    if std::path::Path::new("/sys/class/dmi/id/sys_vendor").exists() {
        if let Ok(content) = std::fs::read_to_string("/sys/class/dmi/id/sys_vendor") {
            let vendor = content.to_lowercase();
            if vendor.contains("vmware") || vendor.contains("virtualbox") ||
               vendor.contains("qemu") || vendor.contains("hyper-v") {
                return true;
            }
        }
    }

    false
}

/// Detects the platform type from OS name
fn detect_platform_type(os_name: &str) -> PlatformType {
    let os_lower = os_name.to_lowercase();

    if os_lower.contains("linux") || os_lower.contains("ubuntu") ||
       os_lower.contains("debian") || os_lower.contains("centos") ||
       os_lower.contains("fedora") || os_lower.contains("rhel") ||
       os_lower.contains("alpine") {
        return PlatformType::Linux;
    }

    if os_lower.contains("mac") || os_lower.contains("darwin") {
        return PlatformType::MacOS;
    }

    if os_lower.contains("windows") {
        return PlatformType::Windows;
    }

    if std::env::consts::FAMILY == "wasm" {
        return PlatformType::WebAssembly;
    }

    PlatformType::Unknown
}

/// Detects available discovery strategies for the platform
fn detect_available_strategies(platform_type: &PlatformType) -> Vec<DiscoveryStrategy> {
    let mut strategies = Vec::with_capacity(4);

    match platform_type {
        PlatformType::Linux => {
            if std::path::Path::new("/proc").exists() {
                strategies.push(DiscoveryStrategy::ProcFs);
            }
            if std::path::Path::new("/sys").exists() {
                strategies.push(DiscoveryStrategy::SysFs);
            }
            if Command::new("dbus-send").output().is_ok() {
                strategies.push(DiscoveryStrategy::DBus);
            }
            if std::path::Path::new("/dev").exists() {
                strategies.push(DiscoveryStrategy::UDev);
            }
            strategies.push(DiscoveryStrategy::NetworkScan);
            strategies.push(DiscoveryStrategy::MockFallback);
        }
        PlatformType::MacOS => {
            strategies.push(DiscoveryStrategy::IOKit);
            strategies.push(DiscoveryStrategy::SystemCalls);
            strategies.push(DiscoveryStrategy::NetworkScan);
            strategies.push(DiscoveryStrategy::MockFallback);
        }
        PlatformType::Windows => {
            if Command::new("wmic").output().is_ok() {
                strategies.push(DiscoveryStrategy::WMI);
            }
            strategies.push(DiscoveryStrategy::Registry);
            strategies.push(DiscoveryStrategy::SystemCalls);
            strategies.push(DiscoveryStrategy::NetworkScan);
            strategies.push(DiscoveryStrategy::MockFallback);
        }
        PlatformType::WebAssembly => {
            strategies.push(DiscoveryStrategy::VirtualFS);
            strategies.push(DiscoveryStrategy::MockFallback);
        }
        PlatformType::Unknown => {
            strategies.push(DiscoveryStrategy::SystemCalls);
            strategies.push(DiscoveryStrategy::MockFallback);
        }
    }

    strategies
}

/// Platform detection result for hardware compatibility
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct PlatformCompatibility {
    /// Whether the platform is fully supported
    pub is_fully_supported: bool,
    /// Platform type
    pub platform_type: PlatformType,
    /// List of supported hardware categories
    pub supported_hardware: Vec<HardwareCategory>,
    /// Recommended discovery strategies
    pub recommended_strategies: Vec<DiscoveryStrategy>,
    /// Any limitations
    pub limitations: Vec<String>,
}

/// Categories of hardware that can be discovered
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub enum HardwareCategory {
    /// CPU and processor devices
    CPU,
    /// GPU and graphics processors
    GPU,
    /// Memory and RAM
    Memory,
    /// Storage devices (HDD, SSD, NVMe)
    Storage,
    /// Network interfaces and adapters
    Network,
    /// USB devices
    USB,
    /// PCI devices
    PCI,
    /// Input devices (keyboard, mouse)
    Input,
    /// Display devices (monitors)
    Display,
    /// Bluetooth devices
    Bluetooth,
    /// Other or custom devices
    Other,
}

impl PlatformCompatibility {
    /// Creates platform compatibility info for current platform
    pub fn current() -> Self {
        let platform = get_platform_info();
        Self::from_platform(&platform)
    }

    /// Creates platform compatibility from platform info
    pub fn from_platform(platform: &PlatformInfo) -> Self {
        let mut supported_hardware = Vec::with_capacity(4);
        let mut limitations = Vec::with_capacity(4);

        supported_hardware.extend(vec![
            HardwareCategory::CPU,
            HardwareCategory::Memory,
            HardwareCategory::Storage,
        ]);

        let recommended_strategies = match platform.platform_type {
            PlatformType::Linux => {
                supported_hardware.extend(vec![
                    HardwareCategory::GPU,
                    HardwareCategory::Network,
                    HardwareCategory::USB,
                    HardwareCategory::PCI,
                    HardwareCategory::Input,
                    HardwareCategory::Display,
                    HardwareCategory::Bluetooth,
                ]);
                vec![
                    DiscoveryStrategy::SysFs,
                    DiscoveryStrategy::ProcFs,
                    DiscoveryStrategy::UDev,
                    DiscoveryStrategy::NetworkScan,
                ]
            }
            PlatformType::MacOS => {
                supported_hardware.extend(vec![
                    HardwareCategory::GPU,
                    HardwareCategory::Network,
                    HardwareCategory::USB,
                    HardwareCategory::PCI,
                    HardwareCategory::Input,
                    HardwareCategory::Display,
                ]);
                vec![
                    DiscoveryStrategy::IOKit,
                    DiscoveryStrategy::SystemCalls,
                    DiscoveryStrategy::NetworkScan,
                ]
            }
            PlatformType::Windows => {
                supported_hardware.extend(vec![
                    HardwareCategory::GPU,
                    HardwareCategory::Network,
                    HardwareCategory::USB,
                    HardwareCategory::PCI,
                    HardwareCategory::Input,
                    HardwareCategory::Display,
                    HardwareCategory::Bluetooth,
                ]);
                vec![
                    DiscoveryStrategy::WMI,
                    DiscoveryStrategy::SystemCalls,
                    DiscoveryStrategy::NetworkScan,
                ]
            }
            PlatformType::WebAssembly => {
                limitations.push("Limited hardware access in WebAssembly environment".to_string());
                vec![
                    DiscoveryStrategy::VirtualFS,
                    DiscoveryStrategy::MockFallback,
                ]
            }
            PlatformType::Unknown => {
                limitations.push("Unknown platform - using fallback strategies".to_string());
                vec![
                    DiscoveryStrategy::SystemCalls,
                    DiscoveryStrategy::MockFallback,
                ]
            }
        };

        Self {
            is_fully_supported: platform.platform_type != PlatformType::Unknown,
            platform_type: platform.platform_type.clone(),
            supported_hardware,
            recommended_strategies,
            limitations,
        }
    }

    /// Checks if a hardware category is supported
    pub fn supports_category(&self, category: &HardwareCategory) -> bool {
        self.supported_hardware.contains(category)
    }
}

use std::collections::HashMap as FxHashMap;