1use crate::{Error, Result};
2use core::time::Duration;
3use file_system::DirectCharacterDevice;
4pub struct Manager<'a> {
5 device: &'a (dyn DirectCharacterDevice + Send + Sync),
6 start_time: Duration,
7}
8
9impl<'a> Manager<'a> {
10 pub fn new(device: &'a (dyn DirectCharacterDevice + Send + Sync)) -> Result<Self> {
11 let start_time = Self::get_current_time_from_device(device)?;
12
13 Ok(Self { device, start_time })
14 }
15
16 pub fn get_current_time_since_startup(&self) -> Result<Duration> {
17 let current_time = self.get_current_time()?;
18
19 Ok(current_time.abs_diff(self.start_time))
20 }
21
22 pub fn get_current_time(&self) -> Result<Duration> {
23 Self::get_current_time_from_device(self.device)
24 }
25
26 fn get_current_time_from_device(device: &dyn DirectCharacterDevice) -> Result<Duration> {
27 let mut current_time = Duration::default();
28
29 let current_time_raw = unsafe {
30 core::slice::from_raw_parts_mut(
31 &mut current_time as *mut Duration as *mut u8,
32 core::mem::size_of::<Duration>(),
33 )
34 };
35
36 device
37 .read(current_time_raw, 0)
38 .map_err(Error::DeviceError)?;
39
40 Ok(current_time)
41 }
42}
43
44pub fn initialize(
45 driver: &'static (dyn DirectCharacterDevice + Send + Sync),
46) -> Result<Manager<'static>> {
47 Manager::new(driver)
48}