Skip to main content

elinux_hwdetect/
lib.rs

1//! A production-ready Rust crate for Embedded Linux hardware detection.
2//!
3//! This crate provides a unified interface to detect various hardware components
4//! and system information on Embedded Linux devices by reading read-only
5//! interfaces like `/proc`, `/sys`, and `/dev`.
6//!
7//! The detection is designed to be robust, non-intrusive, and work on minimal
8//! BusyBox-based systems without requiring root privileges.
9
10pub mod error;
11pub mod devicetree;
12pub mod os;
13pub mod cpu;
14pub mod board;
15pub mod peripherals;
16
17pub use error::{HwDetectError, Result};
18pub use os::OsInfo;
19pub use cpu::CpuInfo;
20pub use board::BoardInfo;
21pub use peripherals::PeripheralInfo;
22pub use devicetree::DeviceTree;
23
24/// The main structure holding all detected hardware and system information.
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct HardwareInfo {
27    pub board: BoardInfo,
28    pub cpu: CpuInfo,
29    pub peripherals: PeripheralInfo,
30    pub os: OsInfo,
31}
32
33impl HardwareInfo {
34    /// Performs a full hardware and system detection.
35    ///
36    /// This function calls the `detect()` method on all sub-modules and
37    /// aggregates the results.
38    pub fn detect() -> Result<Self> {
39        Ok(HardwareInfo {
40            board: BoardInfo::detect().unwrap_or_default(),
41            cpu: CpuInfo::detect()?,
42            peripherals: PeripheralInfo::detect()?,
43            os: OsInfo::detect()?,
44        })
45    }
46}
47
48// Re-export the error type for convenience
49pub type Error = HwDetectError;
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    // A simple smoke test to ensure detection runs without panicking
56    #[test]
57    fn smoke_test_full_detection() {
58        let result = HardwareInfo::detect();
59        assert!(result.is_ok() || result.is_err()); // Should not panic
60        
61        if let Ok(info) = result {
62            println!("Detected Hardware Info: {:#?}", info);
63            assert!(!info.os.kernel_version.is_empty());
64            assert!(info.cpu.core_count > 0);
65        } else {
66            println!("Detection failed (expected on non-Linux/minimal systems): {:?}", result.unwrap_err());
67        }
68    }
69}