ableos/kernel/src/device_tree.rs

84 lines
2.7 KiB
Rust
Raw Normal View History

2023-06-13 11:00:11 +00:00
//! A tree of hardware devices
2023-05-06 11:50:24 +00:00
2023-06-13 11:00:11 +00:00
use {
crate::alloc::string::ToString,
alloc::{string::String, vec::Vec},
core::fmt,
hashbrown::HashMap,
};
/// A device object.
/// TODO define device
2023-05-06 11:50:24 +00:00
pub type Device = xml::XMLElement;
2023-06-13 11:00:11 +00:00
/// A tree of devices
2023-05-15 07:19:34 +00:00
// TODO: alphabatize this list
2023-05-06 11:50:24 +00:00
#[derive(Debug)]
pub struct DeviceTree {
2023-06-13 11:00:11 +00:00
/// The device tree
2023-05-06 11:50:24 +00:00
pub devices: HashMap<String, Vec<Device>>,
}
impl DeviceTree {
2023-06-13 11:00:11 +00:00
/// Build the device tree. Does not populate the device tree
2023-05-06 11:50:24 +00:00
pub fn new() -> Self {
let mut dt = Self {
devices: HashMap::new(),
};
// Human input devices
{
dt.devices.insert("Mice".to_string(), Vec::new());
dt.devices.insert("Keyboards".to_string(), Vec::new());
dt.devices.insert("Controllers".to_string(), Vec::new());
// Human Input Devices that do not fit into other catagories go in this spot
dt.devices.insert("Generic HIDs".to_string(), Vec::new());
}
{
dt.devices.insert("Disk Drives".to_string(), Vec::new());
dt.devices.insert("CD Drives".to_string(), Vec::new());
}
dt.devices.insert("Batteries".to_string(), Vec::new());
dt.devices.insert("Monitors".to_string(), Vec::new());
dt.devices.insert("GPUs".to_string(), Vec::new());
dt.devices.insert("CPUs".to_string(), Vec::new());
dt.devices.insert("USB".to_string(), Vec::new());
dt.devices.insert("Serial Ports".to_string(), Vec::new());
dt.devices.insert("Cameras".to_string(), Vec::new());
dt.devices
.insert("Biometric Devices".to_string(), Vec::new());
dt
}
}
use crate::utils::TAB;
use crate::tab;
2023-05-06 11:50:24 +00:00
impl fmt::Display for DeviceTree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-05-23 10:16:14 +00:00
writeln!(f)?;
2023-05-06 11:50:24 +00:00
for (device_type, devices) in &self.devices {
writeln!(f, "\r{}{}/\r", tab!(1), device_type)?;
2023-05-06 11:50:24 +00:00
for device in devices {
writeln!(f, "{}{}/\r", tab!(2), device.name)?;
2023-05-06 11:50:24 +00:00
for attr in &device.attributes {
writeln!(f, "{}{}\r", tab!(3), attr)?;
2023-05-06 11:50:24 +00:00
}
for child in &device.children {
writeln!(f, "{}{}\r", tab!(3), child.name)?;
2023-05-06 11:50:24 +00:00
for attr in &child.attributes {
writeln!(f, "{}{}\r", tab!(4), attr)?;
2023-05-06 11:50:24 +00:00
}
for child in &child.children {
writeln!(f, "{}{}\r", tab!(4), child.name)?;
2023-05-06 11:50:24 +00:00
for attr in &child.attributes {
writeln!(f, "{}{}\r", tab!(5), attr)?;
2023-05-06 11:50:24 +00:00
}
}
}
}
}
Ok(())
}
}