Skip to content

Commit

Permalink
Refactir time to use generic device instead of time driver and write …
Browse files Browse the repository at this point in the history
…duration type in shared
  • Loading branch information
AlixANNERAUD committed Oct 9, 2024
1 parent e7d5217 commit ab4ac37
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 31 deletions.
148 changes: 148 additions & 0 deletions Modules/Shared/src/Duration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use core::{
ops::{Add, AddAssign, Sub, SubAssign},
time::Duration,
};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Duration_type {
Seconds: u64,
Nanoseconds: u32,
}

impl Duration_type {
pub const fn New(Seconds: u64, Nanoseconds: u32) -> Self {
Duration_type {
Seconds,
Nanoseconds,
}
}

pub fn Get_duration_since(&self, Earlier: &Duration_type) -> Duration_type {
self.Get_duration_since_checked(Earlier).unwrap_or_default()
}

pub fn Get_duration_since_checked(&self, Earlier: &Duration_type) -> Option<Duration_type> {
let self_duration = Duration::new(self.Seconds, self.Nanoseconds);
let earlier_duration = Duration::new(Earlier.Seconds, Earlier.Nanoseconds);
self_duration
.checked_sub(earlier_duration)
.map(|d| Duration_type {
Seconds: d.as_secs(),
Nanoseconds: d.subsec_nanos(),
})
}

pub fn Get_duration_since_saturating(&self, earlier: &Duration_type) -> Duration_type {
let self_duration = Duration::new(self.Seconds, self.Nanoseconds);
let earlier_duration = Duration::new(earlier.Seconds, earlier.Nanoseconds);
let result = self_duration.saturating_sub(earlier_duration);
Duration_type {
Seconds: result.as_secs(),
Nanoseconds: result.subsec_nanos(),
}
}

pub fn Add_checked(&self, Duration: &Duration_type) -> Option<Duration_type> {
let self_duration = Duration::new(self.Seconds, self.Nanoseconds);
let duration = Duration::new(Duration.Seconds, Duration.Nanoseconds);
self_duration.checked_add(duration).map(|d| Duration_type {
Seconds: d.as_secs(),
Nanoseconds: d.subsec_nanos(),
})
}

pub fn Substract_checked(&self, Duration: &Duration_type) -> Option<Duration_type> {
let self_duration = Duration::new(self.Seconds, self.Nanoseconds);
let duration = Duration::new(Duration.Seconds, Duration.Nanoseconds);
self_duration.checked_sub(duration).map(|d| Duration_type {
Seconds: d.as_secs(),
Nanoseconds: d.subsec_nanos(),
})
}

pub fn Add_saturating(&self, Duration: &Duration_type) -> Duration_type {
let self_duration = Duration::new(self.Seconds, self.Nanoseconds);
let duration = Duration::new(Duration.Seconds, Duration.Nanoseconds);
let result = self_duration.saturating_add(duration);
Duration_type {
Seconds: result.as_secs(),
Nanoseconds: result.subsec_nanos(),
}
}

pub fn Substract_saturating(&self, duration: &Duration_type) -> Duration_type {
let self_duration = Duration::new(self.Seconds, self.Nanoseconds);
let duration = Duration::new(duration.Seconds, duration.Nanoseconds);
let result = self_duration.saturating_sub(duration);
Duration_type {
Seconds: result.as_secs(),
Nanoseconds: result.subsec_nanos(),
}
}

pub fn As_seconds(&self) -> u64 {
self.Seconds
}

pub fn As_milliseconds(&self) -> u64 {
self.As_microseconds() as u64 / 1_000
}

pub fn As_microseconds(&self) -> u128 {
self.As_nanoseconds() / 1_000
}

pub fn As_nanoseconds(&self) -> u128 {
u128::from(self.Seconds) * 1_000_000_000 + u128::from(self.Nanoseconds)
}
}

impl Add<&Duration_type> for Duration_type {
type Output = Duration_type;

fn add(self, Duration: &Duration_type) -> Duration_type {
self.Add_checked(Duration)
.expect("Overflow when adding duration")
}
}

impl AddAssign<&Duration_type> for Duration_type {
fn add_assign(&mut self, Duration: &Duration_type) {
*self = self
.Add_checked(Duration)
.expect("Overflow when adding duration");
}
}

impl Sub<&Duration_type> for Duration_type {
type Output = Duration_type;

fn sub(self, Duration: &Duration_type) -> Duration_type {
self.Substract_checked(Duration)
.expect("Overflow when substracting duration")
}
}

impl SubAssign<&Duration_type> for Duration_type {
fn sub_assign(&mut self, Duration: &Duration_type) {
*self = self
.Substract_checked(Duration)
.expect("Overflow when substracting duration");
}
}

impl AsMut<[u8]> for Duration_type {
fn as_mut(&mut self) -> &mut [u8] {
unsafe {
core::slice::from_raw_parts_mut(self as *mut _ as *mut u8, core::mem::size_of::<Self>())
}
}
}

impl AsRef<[u8]> for Duration_type {
fn as_ref(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts(self as *const _ as *const u8, core::mem::size_of::<Self>())
}
}
}
2 changes: 2 additions & 0 deletions Modules/Shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]

mod Duration;
mod Error;
mod Mutable_slice;
mod Mutable_string;
mod Ring_buffer;
mod Size;

pub use Duration::*;
pub use Error::*;
pub use Mutable_slice::*;
pub use Mutable_string::*;
Expand Down
9 changes: 9 additions & 0 deletions Modules/Time/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ version = "0.1.0"
edition = "2021"

[dependencies]
File_system = { path = "../File_system" }
Shared = { path = "../Shared" }

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

[[test]]
name = "Integration"
path = "Tests/Integration.rs"
30 changes: 30 additions & 0 deletions Modules/Time/Tests/Integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]

use File_system::Create_device;
use Shared::Duration_type;

#[test]
fn Test_get_current_time() {
let _ = Time::Initialize(Create_device!(Drivers::Native::Time_driver_type::New()));

let Current_time = Time::Get_instance().Get_current_time().unwrap();

println!("Current time : {:?}", Current_time);

assert_ne!(Current_time, Duration_type::default());
}

#[test]
fn Test_get_current_time_since_startup() {
let _ = Time::Initialize(Create_device!(Drivers::Native::Time_driver_type::New()));

let Current_time = Time::Get_instance()
.Get_current_time_since_startup()
.unwrap();

println!("Time since startup : {:?}", Current_time);

assert_ne!(Current_time, Duration_type::default());
}
7 changes: 0 additions & 7 deletions Modules/Time/src/Driver.rs

This file was deleted.

1 change: 1 addition & 0 deletions Modules/Time/src/Error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub enum Error_type {
Not_initialized,
Already_initialized,
Device_error(File_system::Error_type),
}

pub type Result_type<T> = Result<T, Error_type>;
53 changes: 31 additions & 22 deletions Modules/Time/src/Manager.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,51 @@
use std::time::Duration;
use std::sync::OnceLock;

use crate::{Driver::Driver_trait, Error_type, Result_type};
use File_system::Device_type;
use Shared::Duration_type;

pub static mut Manager: Option<Manager_type> = None;
use crate::{Error_type, Result_type};

pub fn Get_instance() -> &'static Manager_type {
unsafe { Manager.as_ref().expect("Time manager is not initialized") }
}
pub static Manager: OnceLock<Manager_type> = OnceLock::new();

pub fn Is_initialized() -> bool {
unsafe { Manager.is_some() }
pub fn Get_instance() -> &'static Manager_type {
Manager.get().expect("Time manager is not initialized")
}

pub fn Initialize(Driver: Box<dyn Driver_trait>) -> Result_type<&'static Manager_type> {
if Is_initialized() {
return Err(Error_type::Already_initialized);
}
unsafe {
Manager.replace(Manager_type::New(Driver)?);
}
pub fn Initialize(Driver: Device_type) -> Result_type<&'static Manager_type> {
Manager.get_or_init(|| Manager_type::New(Driver).expect("Failed to initialize time manager"));

Ok(Get_instance())
}

pub struct Manager_type {
Driver: Box<dyn Driver_trait>,
Device: Device_type,
Start_time: Duration_type,
}

impl Manager_type {
pub fn New(Driver: Box<dyn Driver_trait>) -> Result_type<Self> {
Ok(Self { Driver })
pub fn New(Device: Device_type) -> Result_type<Self> {
let mut Start_time = Duration_type::default();

Device
.Read(Start_time.as_mut())
.map_err(Error_type::Device_error)?;

Ok(Self { Device, Start_time })
}

pub fn Get_current_time_since_startup(&self) -> Duration {
self.Driver.Get_instant_since_startup()
pub fn Get_current_time_since_startup(&self) -> Result_type<Duration_type> {
let Current_time = self.Get_current_time()?;

Ok(Current_time.Get_duration_since(&self.Start_time))
}

pub fn Get_current_time(&self) -> Duration {
self.Driver.Get_current_time()
pub fn Get_current_time(&self) -> Result_type<Duration_type> {
let mut Current_time = Duration_type::default();

self.Device
.Read(Current_time.as_mut())
.map_err(Error_type::Device_error)?;

Ok(Current_time)
}
}
2 changes: 0 additions & 2 deletions Modules/Time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]

mod Driver;
mod Error;
mod Manager;

pub use Driver::*;
pub use Error::*;
pub use Manager::*;

Expand Down

0 comments on commit ab4ac37

Please sign in to comment.