diff --git a/bsp_linux/main.c b/bsp_linux/main.c index f56f989..2d8ba34 100644 --- a/bsp_linux/main.c +++ b/bsp_linux/main.c @@ -6,6 +6,10 @@ void mission(void); +int get_descriptor_rw() { + return 1; +} + void done() { printf("done.\n"); exit(0); @@ -18,9 +22,6 @@ void rust_eh_personality() { } int main(void) { - printf("std %i sterr %i\n", stdout, stderr); - const char * buffer ="123"; - write(STDOUT_FILENO, buffer, 3); mission(); return 0; } \ No newline at end of file diff --git a/mission_rust/Cargo.lock b/mission_rust/Cargo.lock index c0a15a2..3d7ef28 100644 --- a/mission_rust/Cargo.lock +++ b/mission_rust/Cargo.lock @@ -2,6 +2,40 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "descriptor_procmac" +version = "0.1.0" +dependencies = [ + "quote", +] + [[package]] name = "mission_rust" version = "0.1.0" +dependencies = [ + "descriptor_procmac", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/mission_rust/Cargo.toml b/mission_rust/Cargo.toml index d39fbfe..8dc6e3d 100644 --- a/mission_rust/Cargo.toml +++ b/mission_rust/Cargo.toml @@ -12,6 +12,5 @@ panic = 'abort' [profile.release] panic = 'abort' -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] +descriptor_procmac = { path = "./descriptor_procmac" } \ No newline at end of file diff --git a/mission_rust/descriptor_procmac/Cargo.toml b/mission_rust/descriptor_procmac/Cargo.toml new file mode 100644 index 0000000..24704c8 --- /dev/null +++ b/mission_rust/descriptor_procmac/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "descriptor_procmac" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +quote = "1.0" \ No newline at end of file diff --git a/mission_rust/descriptor_procmac/src/lib.rs b/mission_rust/descriptor_procmac/src/lib.rs new file mode 100644 index 0000000..b04c0b5 --- /dev/null +++ b/mission_rust/descriptor_procmac/src/lib.rs @@ -0,0 +1,41 @@ +#![crate_type = "proc-macro"] +extern crate proc_macro; +use proc_macro::TokenStream; + +#[proc_macro] +pub fn Device_Descriptor(input: TokenStream) -> TokenStream { + let first_token = input.into_iter().next().unwrap(); // Do proper error handling! + let raw_string = first_token.to_string(); + let raw_string_len = raw_string.len(); + let identifier = &first_token.to_string()[1..raw_string_len-1]; + println!("first: {}", identifier); + // let value = match first_token { + // Literal { symbol, kind: Str, ..} => {println!("yes: {}", symbol); "12"}, + // _ => "13", + // }; + // let string_value = match litrs::StringLit::try_from(first_token) { + // Ok(string_lit) => string_lit, + // Err(e) => return e.to_compile_error(), + // }; + let getter_name = quote::format_ident!("get_descriptor_{}", identifier); + let struct_name = quote::format_ident!("Descriptor_{}", identifier); + let tokens = quote::quote!( + extern "C" { + pub fn #getter_name() -> core::ffi::c_int; + pub fn write(fd: core::ffi::c_int, buffer: *const core::ffi::c_void, count: usize) -> core::ffi::c_int; + } + + struct #struct_name {} + + impl Hardware_Write for #struct_name { + fn write(&mut self, buf: &[u8]) -> Result{ + unsafe { + let fd = #getter_name( ); + let written = write(fd, buf.as_ptr() as *const core::ffi::c_void, buf.len()); + Ok(written) + } + } + } + ); + tokens.into() +} diff --git a/mission_rust/src/lib.rs b/mission_rust/src/lib.rs index 6ce067a..ee59741 100644 --- a/mission_rust/src/lib.rs +++ b/mission_rust/src/lib.rs @@ -11,6 +11,14 @@ use core::panic::PanicInfo; use fsrc::objectmanager::SystemObjectIF; use fsrc::*; +use descriptor_procmac::Device_Descriptor; + +trait Hardware_Write { + fn write(&mut self, buf: &[u8]) -> Result; +} + +Device_Descriptor!("rw"); + extern "C" { fn done(); } @@ -70,6 +78,9 @@ extern "C" fn rust_alloc_failed(){ #[no_mangle] extern "C" fn rust_main() { sifln!("Rust startup 🚀"); + let mut rw = Descriptor_rw{}; + let data: &[u8] = &[0x30,0x31,10]; + _ = rw.write(data); mission(); sifln!("Mission done"); }