2023-06-13 06:00:11 -05:00
|
|
|
//! A tree of hardware devices
|
2023-05-06 06:50:24 -05:00
|
|
|
|
2024-09-30 15:45:57 -05:00
|
|
|
use {alloc::vec::Vec, core::fmt, hashbrown::HashMap};
|
2024-09-13 18:11:23 -05:00
|
|
|
|
2023-06-13 06:00:11 -05:00
|
|
|
/// A device object.
|
|
|
|
/// TODO define device
|
2023-05-06 06:50:24 -05:00
|
|
|
pub type Device = xml::XMLElement;
|
|
|
|
|
2023-06-13 06:00:11 -05:00
|
|
|
/// A tree of devices
|
2023-07-15 07:47:46 -05:00
|
|
|
// TODO: alphabetize this list
|
2023-05-06 06:50:24 -05:00
|
|
|
#[derive(Debug)]
|
2024-09-30 15:45:57 -05:00
|
|
|
pub struct DeviceTree<'a> {
|
2023-06-13 06:00:11 -05:00
|
|
|
/// The device tree
|
2024-09-30 15:45:57 -05:00
|
|
|
pub devices: HashMap<&'a str, Vec<Device>>,
|
2023-05-06 06:50:24 -05:00
|
|
|
}
|
2024-09-30 15:45:57 -05:00
|
|
|
impl<'a> DeviceTree<'a> {
|
2023-06-13 06:00:11 -05:00
|
|
|
/// Build the device tree. Does not populate the device tree
|
2023-05-06 06:50:24 -05:00
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut dt = Self {
|
|
|
|
devices: HashMap::new(),
|
|
|
|
};
|
2024-09-13 18:11:23 -05:00
|
|
|
device_tree!(
|
|
|
|
dt,
|
|
|
|
[
|
|
|
|
"Mice",
|
|
|
|
"Keyboards",
|
|
|
|
"Controllers",
|
|
|
|
"Generic HIDs",
|
|
|
|
"Disk Drives",
|
|
|
|
"CD Drives",
|
|
|
|
"Batteries",
|
|
|
|
"Monitors",
|
|
|
|
"GPUs",
|
|
|
|
"CPUs",
|
|
|
|
"USB",
|
|
|
|
"Serial Ports",
|
|
|
|
"Cameras",
|
|
|
|
"Biometric Devices",
|
|
|
|
]
|
|
|
|
);
|
2023-05-06 06:50:24 -05:00
|
|
|
dt
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 18:11:23 -05:00
|
|
|
use crate::{device_tree, tab, utils::TAB};
|
2024-09-30 15:45:57 -05:00
|
|
|
impl<'a> fmt::Display for DeviceTree<'a> {
|
2023-05-06 06:50:24 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-05-23 05:16:14 -05:00
|
|
|
writeln!(f)?;
|
2023-05-06 06:50:24 -05:00
|
|
|
for (device_type, devices) in &self.devices {
|
2023-07-09 06:00:16 -05:00
|
|
|
writeln!(f, "\r{}{}/\r", tab!(1), device_type)?;
|
2023-05-06 06:50:24 -05:00
|
|
|
for device in devices {
|
2023-07-09 06:00:16 -05:00
|
|
|
writeln!(f, "{}{}/\r", tab!(2), device.name)?;
|
2023-05-06 06:50:24 -05:00
|
|
|
for attr in &device.attributes {
|
2023-07-09 06:00:16 -05:00
|
|
|
writeln!(f, "{}{}\r", tab!(3), attr)?;
|
2023-05-06 06:50:24 -05:00
|
|
|
}
|
|
|
|
for child in &device.children {
|
2023-07-09 06:00:16 -05:00
|
|
|
writeln!(f, "{}{}\r", tab!(3), child.name)?;
|
2023-05-06 06:50:24 -05:00
|
|
|
for attr in &child.attributes {
|
2023-07-09 06:00:16 -05:00
|
|
|
writeln!(f, "{}{}\r", tab!(4), attr)?;
|
2023-05-06 06:50:24 -05:00
|
|
|
}
|
|
|
|
for child in &child.children {
|
2023-07-09 06:00:16 -05:00
|
|
|
writeln!(f, "{}{}\r", tab!(4), child.name)?;
|
2023-05-06 06:50:24 -05:00
|
|
|
for attr in &child.attributes {
|
2023-07-09 06:00:16 -05:00
|
|
|
writeln!(f, "{}{}\r", tab!(5), attr)?;
|
2023-05-06 06:50:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|