Skip to content

Commit

Permalink
Merge pull request #50 from Xila-Project/Feature/Command_line_shell
Browse files Browse the repository at this point in the history
Add example to launch Xila and small refactoring
  • Loading branch information
AlixANNERAUD authored Nov 30, 2024
2 parents d546237 + 2a45d97 commit 5e6856a
Show file tree
Hide file tree
Showing 27 changed files with 450 additions and 186 deletions.
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Drivers = { path = "Modules/Drivers", optional = true }
Memory = { path = "Modules/Memory", optional = true }
WASM = { path = "Modules/Executables/WASM", optional = true }
Command_line_shell = { path = "Modules/Executables/Shell/Command_line", optional = true }
Executable = { path = "Modules/Executable", optional = true }
File_system = { path = "Modules/File_system", optional = true }
Host_bindings = { path = "Modules/Bindings/Host", optional = true }

[build-dependencies]
Target = { path = "Modules/Target", optional = true }
Expand All @@ -69,5 +72,13 @@ Host = [
"dep:WASM",
"dep:Command_line_shell",
"dep:Target",
"dep:Executable",
"dep:File_system",
"dep:Host_bindings",
]
WASM = ["dep:WASM_bindings"]

[[example]]
name = "Native"
path = "Examples/Native.rs"
required-features = ["Host"]
107 changes: 107 additions & 0 deletions Examples/Native.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]

use Command_line_shell::Shell_executable_type;
use Executable::Standard_type;
use File_system::{Create_device, Create_file_system, Mode_type};
use WASM::WASM_device_type;

fn main() {
// - Initialize the system
// Initialize the task manager
Task::Initialize().unwrap();
// Initialize the users manager
Users::Initialize().unwrap();
// Initialize the time manager
Time::Initialize(Create_device!(Drivers::Native::Time_driver_type::New())).unwrap();
// - Initialize the graphics manager
// - - Initialize the graphics driver
const Resolution: Graphics::Point_type = Graphics::Point_type::New(800, 600);
let (Screen, Pointer) = Drivers::Native::Window_screen::New(Resolution).unwrap();
// - - Initialize the graphics manager
Graphics::Initialize();
// - - Add a screen
const Buffer_size: usize = Graphics::Get_recommended_buffer_size(&Resolution);
let _ = Graphics::Get_instance()
.Create_display::<Buffer_size>(Screen, Pointer, true)
.unwrap();

// - Initialize the file system
// Create a memory device
let Drive = Create_device!(Drivers::Native::File_drive_device_type::New(&"./Drive"));
// Mount the file system
let File_system = match LittleFS::File_system_type::New(Drive.clone(), 256) {
Ok(File_system) => File_system,
// If the file system is not found, format it
Err(_) => {
Drive
.Set_position(&File_system::Position_type::Start(0))
.unwrap();

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

LittleFS::File_system_type::New(Drive, 256).unwrap()
}
};
// Initialize the virtual file system
Virtual_file_system::Initialize(Create_file_system!(File_system));

// - - Mount the devices
let Task = Task::Get_instance().Get_current_task_identifier().unwrap();

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

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

let _ = Virtual_file_system::Get_instance().Create_directory(&"/Devices", Task);

Drivers::Native::Console::Mount_devices(Task, Virtual_file_system::Get_instance()).unwrap();
// Initialize the virtual machine
Virtual_machine::Initialize(&[&Host_bindings::Graphics_bindings]);

// - Execute the shell
// - - Open the standard input, output and error
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(),
);
// - - Set the environment variables
Task::Get_instance()
.Set_environment_variable(Task, "Paths", "/")
.unwrap();

Task::Get_instance()
.Set_environment_variable(Task, "Host", "xila")
.unwrap();
// - - Execute the shell
let _ = Executable::Execute("/Shell", "".to_string(), Standard)
.unwrap()
.Join()
.unwrap();

Virtual_file_system::Uninitialize();
}
18 changes: 10 additions & 8 deletions Modules/Bindings/Host/Tests/Graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ fn Integration_test() {
let Memory_device = Create_device!(Memory_device_type::<512>::New(1024 * 512));
LittleFS::File_system_type::Format(Memory_device.clone(), 512).unwrap();

let Virtual_file_system = Virtual_file_system::Initialize(Create_file_system!(
LittleFS::File_system_type::New(Memory_device, 256).unwrap()
))
Virtual_file_system::Initialize(Create_file_system!(LittleFS::File_system_type::New(
Memory_device,
256
)
.unwrap()))
.unwrap();

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

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

Virtual_machine::Initialize(&[&Host_bindings::Graphics_bindings]);

Expand All @@ -61,23 +63,23 @@ fn Integration_test() {

let _Calendar = unsafe { lvgl::lv_calendar_create(Screen_object) };

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

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

let Standard_error = Virtual_file_system
let Standard_error = Virtual_file_system::Get_instance()
.Open(
&"/Devices/Standard_out",
File_system::Mode_type::Write_only.into(),
Expand Down
6 changes: 4 additions & 2 deletions Modules/Bindings/Host/src/Graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ pub unsafe fn Call(
let Lock = Instance.Lock();

if Lock.is_ok() {
let _ = Pointer_table.get_or_init(Pointer_table_type::New);
let Pointer_table_reference = &raw mut Pointer_table;

let Pointer_table_reference = Pointer_table.get_mut().unwrap();
let _ = (*Pointer_table_reference).get_or_init(Pointer_table_type::New);

let Pointer_table_reference = (*Pointer_table_reference).get_mut().unwrap();

Generated_bindings::Call_function(
Environment,
Expand Down
12 changes: 6 additions & 6 deletions Modules/Drivers/src/Native/Devices/Console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,25 @@ pub fn Mount_devices(
Virtual_file_system: &Virtual_file_system_type,
) -> Result<(), String> {
Virtual_file_system
.Mount_device(
.Mount_static_device(
Task,
"/Devices/Standard_in",
&"/Devices/Standard_in",
Create_device!(Standard_in_device_type),
)
.map_err(|Error| format!("Error adding standard in device: {:?}", Error))?;

Virtual_file_system
.Mount_device(
.Mount_static_device(
Task,
"/Devices/Standard_out",
&"/Devices/Standard_out",
Create_device!(Standard_out_device_type),
)
.map_err(|Error| format!("Error adding standard out device: {:?}", Error))?;

Virtual_file_system
.Mount_device(
.Mount_static_device(
Task,
"/Devices/Standard_error",
&"/Devices/Standard_error",
Create_device!(Standard_error_device_type),
)
.map_err(|Error| format!("Error adding standard error device: {:?}", Error))?;
Expand Down
57 changes: 49 additions & 8 deletions Modules/Drivers/src/Native/Devices/Drive_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl File_drive_device_type {
.read(true)
.write(true)
.create(true)
.truncate(true)
.truncate(false)
.open(Path)
.expect("Error opening file");

Expand Down Expand Up @@ -44,13 +44,7 @@ impl Device_trait for File_drive_device_type {
}

fn Get_size(&self) -> File_system::Result_type<File_system::Size_type> {
self.0
.try_read()
.map_err(|_| Error_type::Ressource_busy)?
.metadata()
.map(|Metadata| Metadata.len())
.map(Size_type::New)
.map_err(|Error| Error.into())
Ok((1024 * 1024 * 1024 * 4_usize).into())
}

fn Set_position(
Expand Down Expand Up @@ -78,4 +72,51 @@ impl Device_trait for File_drive_device_type {
fn Erase(&self) -> File_system::Result_type<()> {
Ok(())
}

fn Get_block_size(&self) -> File_system::Result_type<usize> {
Ok(4096)
}
}

#[cfg(test)]
mod Tests {
use super::*;
use File_system::Device_trait;

#[test]
fn Test_read_write() {
let File = File_drive_device_type::New(&"./Test");

let Data = [1, 2, 3, 4, 5];

assert_eq!(File.Write(&Data).unwrap(), Size_type::New(5));

File.Set_position(&File_system::Position_type::Start(0))
.unwrap();

let mut Buffer = [0; 5];

assert_eq!(File.Read(&mut Buffer).unwrap(), Size_type::New(5));
assert_eq!(Buffer, Data);
}

#[test]
fn Test_read_write_at_position() {
let File = File_drive_device_type::New(&"./Test");

File.Set_position(&File_system::Position_type::Start(10))
.unwrap();

let Data = [1, 2, 3, 4, 5];

assert_eq!(File.Write(&Data).unwrap(), Size_type::New(5));

File.Set_position(&File_system::Position_type::Start(10))
.unwrap();

let mut Buffer = [0; 5];

assert_eq!(File.Read(&mut Buffer).unwrap(), Size_type::New(5));
assert_eq!(Buffer, Data);
}
}
4 changes: 2 additions & 2 deletions Modules/Drivers/src/Native/Devices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub fn Mount_devices(
let (Screen, Pointer) = Window_screen::New(Resolution)?;

Virtual_file_system
.Mount_device(Task, "/Devices/Screen", Screen)
.Mount_static_device(Task, &"/Devices/Screen", Screen)
.map_err(|Error| format!("Error adding screen device: {:?}", Error))?;

Virtual_file_system
.Mount_device(Task, "/Devices/Pointer", Pointer)
.Mount_static_device(Task, &"/Devices/Pointer", Pointer)
.map_err(|Error| format!("Error adding pointer device: {:?}", Error))?;

Console::Mount_devices(Task, Virtual_file_system)?;
Expand Down
23 changes: 9 additions & 14 deletions Modules/Executables/Shell/Command_line/Tests/Integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,29 @@ fn Integration_test() {

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

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

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

Virtual_file_system
.Mount_device(Task, "/Shell", Create_device!(Shell_executable_type))
Virtual_file_system::Get_instance()
.Mount_static_device(Task, &"/Shell", Create_device!(Shell_executable_type))
.unwrap();

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

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

let Standard_in = Virtual_file_system
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
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
let Standard_error = Virtual_file_system::Get_instance()
.Open(
&"/Devices/Standard_error",
Mode_type::Write_only.into(),
Expand All @@ -57,17 +56,13 @@ fn Integration_test() {
Standard_out,
Standard_error,
Task,
Virtual_file_system,
Virtual_file_system::Get_instance(),
);

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

Task_instance
.Set_environment_variable(Task, "User", "alix_anneraud")
.unwrap();

Task_instance
.Set_environment_variable(Task, "Host", "xila")
.unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::{Result_type, Shell_type};

impl Shell_type {
pub fn Authenticate(&mut self) -> Result_type<String> {
self.Standard.Print("Username: ");
self.Standard.Out_flush();

let mut Username = String::new();
self.Standard.Read_line(&mut Username);

self.Standard.Print("Password: ");
self.Standard.Out_flush();

let mut Password = String::new();
self.Standard.Read_line(&mut Password);

Ok(Username)
}
}
Loading

0 comments on commit 5e6856a

Please sign in to comment.