Skip to content

Commit

Permalink
Add Terminal module with integration tests and device handling; updat…
Browse files Browse the repository at this point in the history
…e Cargo.toml to include Terminal
  • Loading branch information
AlixANNERAUD committed Dec 19, 2024
1 parent 6e2154e commit da65f51
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"Modules/Executable",
"Modules/Executables/WASM",
"Modules/Executables/Shell/Graphical",
"Modules/Executables/Terminal",
]
exclude = [
"Modules/Virtual_machine/Tests/WASM_test",
Expand Down
23 changes: 23 additions & 0 deletions Modules/Executables/Terminal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "Terminal"
version = "0.1.0"
edition = "2021"

[dependencies]
Graphics = { path = "../../Graphics" }
File_system = { path = "../../File_system" }
Virtual_file_system = { path = "../../Virtual_file_system" }
Executable = { path = "../../Executable" }
Task = { path = "../../Task" }
Users = { path = "../../Users" }
Shared = { path = "../../Shared" }
Time = { path = "../../Time" }

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

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

use Executable::Standard_type;
use File_system::{Create_device, Create_file_system, Memory_device_type, Mode_type};
use Terminal::Terminal_executable_type;

#[cfg(target_os = "linux")]
#[ignore]
#[test]
fn main() {
use Drivers::Native::Window_screen;
use Graphics::{Get_minimal_buffer_size, Input_type_type, Point_type};

// - Initialize the task manager.
let Task_instance = Task::Initialize().unwrap();

// - Initialize the user manager.
let _ = Users::Initialize();

// - Initialize the time manager.
let _ = Time::Initialize(Create_device!(Drivers::Native::Time_driver_type::New()));

// - Initialize the graphics manager.

const Resolution: Point_type = Point_type::New(800, 480);

let (Screen_device, Pointer_device, Keyboard_device) = Window_screen::New(Resolution).unwrap();

const Buffer_size: usize = Get_minimal_buffer_size(&Resolution);

Graphics::Initialize(
Screen_device,
Pointer_device,
Input_type_type::Pointer,
Buffer_size,
true,
);

Graphics::Get_instance()
.Add_input_device(Keyboard_device, Input_type_type::Keypad)
.unwrap();

// - Initialize the virtual file system.
let Memory_device = Create_device!(Memory_device_type::<512>::New(1024 * 512));

LittleFS::File_system_type::Format(Memory_device.clone(), 256).unwrap();

let File_system = LittleFS::File_system_type::New(Memory_device, 256).unwrap();

Virtual_file_system::Initialize(Create_file_system!(File_system)).unwrap();

let Task = Task_instance.Get_current_task_identifier().unwrap();

Virtual_file_system::Get_instance()
.Mount_static_device(Task, &"/Terminal", Create_device!(Terminal_executable_type))
.unwrap();

Virtual_file_system::Get_instance()
.Create_directory(&"/Devices", Task)
.unwrap();

Drivers::Native::Console::Mount_devices(Task, Virtual_file_system::Get_instance()).unwrap();

let Standard_in = Virtual_file_system::Get_instance()
.Open(&"/Devices/Standard_in", Mode_type::Read_only.into(), Task)
.unwrap();

let Standard_out = Virtual_file_system::Get_instance()
.Open(&"/Devices/Standard_out", Mode_type::Write_only.into(), Task)
.unwrap();

let Standard_error = Virtual_file_system::Get_instance()
.Open(
&"/Devices/Standard_error",
Mode_type::Write_only.into(),
Task,
)
.unwrap();

let Standard = Standard_type::New(
Standard_in,
Standard_out,
Standard_error,
Task,
Virtual_file_system::Get_instance(),
);

Task_instance
.Set_environment_variable(Task, "Paths", "/")
.unwrap();

Task_instance
.Set_environment_variable(Task, "Host", "xila")
.unwrap();

let Result = Executable::Execute("/Terminal", "".to_string(), Standard)
.unwrap()
.Join()
.unwrap();

assert!(Result == 0);
}
37 changes: 37 additions & 0 deletions Modules/Executables/Terminal/src/Device.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use Executable::Read_data_type;
use File_system::Device_trait;

use crate::Main::Main;

pub struct Terminal_executable_type;

impl Device_trait for Terminal_executable_type {
fn Read(&self, Buffer: &mut [u8]) -> File_system::Result_type<File_system::Size_type> {
let Read_data: &mut Read_data_type = Buffer
.try_into()
.map_err(|_| File_system::Error_type::Invalid_parameter)?;

*Read_data = Read_data_type::New(Main, 1024 * 32);

Ok(size_of::<Read_data_type>().into())
}

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

fn Get_size(&self) -> File_system::Result_type<File_system::Size_type> {
Err(File_system::Error_type::Unsupported_operation)
}

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

fn Flush(&self) -> File_system::Result_type<()> {
Err(File_system::Error_type::Unsupported_operation)
}
}
37 changes: 37 additions & 0 deletions Modules/Executables/Terminal/src/Error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use core::result::Result;
use core::{fmt::Display, num::NonZeroU8};
use std::num::NonZeroUsize;

pub type Result_type<T> = Result<T, Error_type>;

#[derive(Debug, Clone)]
#[repr(u8)]
pub enum Error_type {
Graphics(Graphics::Error_type),
}

impl Error_type {
pub fn Get_discriminant(&self) -> NonZeroU8 {
unsafe { *(self as *const Self as *const NonZeroU8) }
}
}

impl From<Graphics::Error_type> for Error_type {
fn from(Error: Graphics::Error_type) -> Self {
Self::Graphics(Error)
}
}

impl Display for Error_type {
fn fmt(&self, Formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
Self::Graphics(Error) => write!(Formatter, "Graphics: {}", Error),
}
}
}

impl From<Error_type> for NonZeroUsize {
fn from(Error: Error_type) -> Self {
Error.Get_discriminant().into()
}
}
30 changes: 30 additions & 0 deletions Modules/Executables/Terminal/src/Main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use core::num::NonZeroUsize;

use Executable::Standard_type;
use Graphics::Window_type;

use crate::Error::Result_type;

pub struct Terminal_type {
_Standard: Standard_type,
Window: Window_type,
}

impl Terminal_type {
pub fn New(Standard: Standard_type) -> Result_type<Self> {
let Window = Graphics::Get_instance().Create_window()?;

Ok(Self {
_Standard: Standard,
Window,
})
}

pub fn Main(&mut self, _: String) -> Result<(), NonZeroUsize> {
Ok(())
}
}

pub fn Main(Standard: Standard_type, Arguments: String) -> Result<(), NonZeroUsize> {
Terminal_type::New(Standard)?.Main(Arguments)
}
11 changes: 11 additions & 0 deletions Modules/Executables/Terminal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]

mod Device;

mod Main;

mod Error;

pub use Device::*;

0 comments on commit da65f51

Please sign in to comment.