Skip to content

Commit

Permalink
Merge pull request #40 from Xila-Project/Feature/Time
Browse files Browse the repository at this point in the history
Feature/Time
  • Loading branch information
AlixANNERAUD authored Oct 9, 2024
2 parents e7d5217 + 9c05349 commit 19cf0c5
Show file tree
Hide file tree
Showing 19 changed files with 390 additions and 67 deletions.
5 changes: 4 additions & 1 deletion Modules/ABI/src/Time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use Time::Get_instance;
#[no_mangle]
pub unsafe extern "C" fn Xila_instant_since_startup_microseconds(Results: *mut u128) {
unsafe {
*Results = Get_instance().Get_current_time_since_startup().as_micros();
*Results = Get_instance()
.Get_current_time_since_startup()
.unwrap_or_default()
.As_microseconds();
}
}
50 changes: 34 additions & 16 deletions Modules/Drivers/src/Native/Time.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
use std::time::SystemTime;
use std::time::{SystemTime, UNIX_EPOCH};

pub struct Time_driver_type {
Start_time: SystemTime,
}
use File_system::{Device_trait, Error_type, Result_type, Size_type};
use Shared::Duration_type;

pub struct Time_driver_type;

impl Time_driver_type {
pub fn New() -> Self {
Self {
Start_time: SystemTime::now(),
}
Self {}
}
}

impl Time::Driver_trait for Time_driver_type {
fn Get_instant_since_startup(&self) -> Time::Duration_type {
self.Start_time
.elapsed()
.expect("Failed to get elapsed time")
impl Device_trait for Time_driver_type {
fn Read(&self, Buffer: &mut [u8]) -> Result_type<Size_type> {
let Duration = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|_| Error_type::Internal_error)?;

let Duration = Duration_type::New(Duration.as_secs(), Duration.subsec_nanos());

Buffer.copy_from_slice(Duration.as_ref());

Ok(Buffer.len().into())
}

fn Write(&self, _: &[u8]) -> Result_type<File_system::Size_type> {
Err(Error_type::Unsupported_operation)
}

fn Get_size(&self) -> File_system::Result_type<File_system::Size_type> {
Ok(size_of::<Duration_type>().into())
}

fn Set_position(
&self,
_: &File_system::Position_type,
) -> File_system::Result_type<File_system::Size_type> {
Err(Error_type::Unsupported_operation)
}

fn Get_current_time(&self) -> Time::Duration_type {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Failed to get current time")
fn Flush(&self) -> File_system::Result_type<()> {
Err(Error_type::Unsupported_operation)
}
}
1 change: 0 additions & 1 deletion Modules/File_system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Shared = { path = "../Shared" }

[dev-dependencies]
Drivers = { path = "../Drivers" }
Time = { path = "../Time" }

[features]
std = []
Expand Down
3 changes: 2 additions & 1 deletion Modules/File_system/src/Error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{num::NonZeroU32, sync::PoisonError};

pub type Result_type<T> = std::result::Result<T, Error_type>;

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Clone, Copy, Eq)]
#[repr(C)]
pub enum Error_type {
Failed_to_initialize_file_system = 1,
Expand Down Expand Up @@ -43,6 +43,7 @@ pub enum Error_type {
Corrupted,
No_memory,
No_space_left,
Time_error,
Other,
}

Expand Down
12 changes: 6 additions & 6 deletions Modules/File_system/src/Time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use Shared::Duration_type;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
Expand All @@ -12,16 +12,16 @@ impl Time_type {
}
}

impl From<Duration> for Time_type {
fn from(Duration: Duration) -> Self {
impl From<Duration_type> for Time_type {
fn from(Duration: Duration_type) -> Self {
Self {
Seconds: Duration.as_secs(),
Seconds: Duration.As_seconds(),
}
}
}

impl From<Time_type> for Duration {
impl From<Time_type> for Duration_type {
fn from(Time: Time_type) -> Self {
Self::from_secs(Time.Seconds)
Duration_type::New(Time.Seconds, 0)
}
}
5 changes: 4 additions & 1 deletion Modules/Graphics/src/Manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ impl Drop for Manager_type {
}

extern "C" fn Binding_tick_callback_function() -> u32 {
Time::Get_instance().Get_current_time().as_millis() as u32
Time::Get_instance()
.Get_current_time()
.unwrap_or_default()
.As_milliseconds() as u32
}

impl Manager_type {
Expand Down
5 changes: 4 additions & 1 deletion Modules/LittleFS/src/File.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ impl File_type {
Cache_size: usize,
) -> Result_type<Self> {
// - Create or get the metadata
let Current_time: Time_type = Time::Get_instance().Get_current_time().into();
let Current_time: Time_type = Time::Get_instance()
.Get_current_time()
.map_err(|_| Error_type::Time_error)?
.into();

let Metadata = if Flags.Get_open().Get_create() {
Metadata_type::Get_default(Task, Type_type::File, Current_time)
Expand Down
13 changes: 8 additions & 5 deletions Modules/LittleFS/src/File_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ impl File_system_traits for File_system_type {
let (File_system, Open_files, Open_directories) =
Self::Borrow_mutable_inner_2_splited(&mut Inner);

let Current_time: Time_type = Time::Get_instance().Get_current_time().into();

// TODO : Find a way to get the metadata of the directories
if Open_directories.get_mut(&File).is_some() {
let Current_time: Time_type = Time::Get_instance().Get_current_time().unwrap().into();

Ok(Statistics_type::New(
File_system_identifier_type::New(0),
Inode_type::New(0),
Expand Down Expand Up @@ -476,7 +476,10 @@ impl File_system_traits for File_system_type {

Directory_type::Create_directory(&mut Inner.File_system, Path)?;

let Current_time: Time_type = Time::Get_instance().Get_current_time().into();
let Current_time: Time_type = Time::Get_instance()
.Get_current_time()
.map_err(|_| Error_type::Time_error)?
.into();

let Metadata = Metadata_type::Get_default(Task, Type_type::Directory, Current_time)
.ok_or(Error_type::Invalid_parameter)?;
Expand Down Expand Up @@ -534,7 +537,7 @@ mod Tests {

use std::sync::Arc;

use File_system::Tests::Memory_device_type;
use File_system::{Create_device, Tests::Memory_device_type};

use super::*;

Expand All @@ -549,7 +552,7 @@ mod Tests {
let _ = Task::Get_instance().Register_task();
}

let _ = Time::Initialize(Box::new(Drivers::Native::Time_driver_type::New()));
let _ = Time::Initialize(Create_device!(Drivers::Native::Time_driver_type::New()));

let Mock_device = Memory_device_type::<512>::New(2048 * 512);

Expand Down
Loading

0 comments on commit 19cf0c5

Please sign in to comment.