ableos/kernel/src/device_tree.rs

74 lines
2.1 KiB
Rust

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