1 use crate::filesystem::sysfs::devices::device_register; 2 use core::{any::Any, fmt::Debug}; 3 4 pub mod bus; 5 pub mod driver; 6 7 /// @brief: 设备类型 8 #[allow(dead_code)] 9 #[derive(Debug, Eq, PartialEq)] 10 pub enum DeviceType { 11 Bus, 12 Net, 13 Gpu, 14 Input, 15 Block, 16 Rtc, 17 Serial, 18 Intc, 19 PlatformDev, 20 } 21 22 /// @brief: 设备标识符类型 23 #[derive(Debug, Clone, Hash, PartialOrd, PartialEq, Ord, Eq)] 24 pub struct IdTable(&'static str, u32); 25 26 /// @brief: 设备标识符操作方法集 27 impl IdTable { 28 /// @brief: 创建一个新的设备标识符 29 /// @parameter name: 设备名 30 /// @parameter id: 设备id 31 /// @return: 设备标识符 32 pub fn new(name: &'static str, id: u32) -> IdTable { 33 Self(name, id) 34 } 35 } 36 37 /// @brief: 设备当前状态 38 #[derive(Debug, Clone, Copy)] 39 pub enum DeviceState { 40 NotInitialized = 0, 41 Initialized = 1, 42 UnDefined = 2, 43 } 44 45 /// @brief: 设备错误类型 46 #[derive(Debug, Copy, Clone)] 47 pub enum DeviceError { 48 DriverExists, // 设备已存在 49 DeviceExists, // 驱动已存在 50 InitializeFailed, // 初始化错误 51 NoDeviceForDriver, // 没有合适的设备匹配驱动 52 NoDriverForDevice, // 没有合适的驱动匹配设备 53 RegisterError, // 注册失败 54 } 55 56 /// @brief: 将u32类型转换为设备状态类型 57 impl From<u32> for DeviceState { 58 fn from(state: u32) -> Self { 59 match state { 60 0 => DeviceState::NotInitialized, 61 1 => DeviceState::Initialized, 62 _ => todo!(), 63 } 64 } 65 } 66 67 /// @brief: 将设备状态转换为u32类型 68 impl From<DeviceState> for u32 { 69 fn from(state: DeviceState) -> Self { 70 match state { 71 DeviceState::NotInitialized => 0, 72 DeviceState::Initialized => 1, 73 DeviceState::UnDefined => 2, 74 } 75 } 76 } 77 78 /// @brief: 所有设备都应该实现该trait 79 pub trait Device: Any + Send + Sync + Debug { 80 /// @brief: 获取设备类型 81 /// @parameter: None 82 /// @return: 实现该trait的设备所属类型 83 fn get_type(&self) -> DeviceType; 84 85 /// @brief: 获取设备标识 86 /// @parameter: None 87 /// @return: 该设备唯一标识 88 fn get_id_table(&self) -> IdTable; 89 90 /// @brief: 设备注册 91 /// @parameter: name: 设备名 92 /// @return: 操作成功,返回(),操作失败,返回错误码 93 fn register_device(&self, name: &str) -> Result<(), DeviceError> { 94 match device_register(name) { 95 Ok(_) => Ok(()), 96 Err(_) => Err(DeviceError::RegisterError), 97 } 98 } 99 } 100