forked from ROMEO/obsw
procmac to not forget writing open calls
This commit is contained in:
parent
02a7d525be
commit
49c19ba675
@ -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;
|
||||
}
|
34
mission_rust/Cargo.lock
generated
34
mission_rust/Cargo.lock
generated
@ -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"
|
||||
|
@ -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" }
|
10
mission_rust/descriptor_procmac/Cargo.toml
Normal file
10
mission_rust/descriptor_procmac/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "descriptor_procmac"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
quote = "1.0"
|
41
mission_rust/descriptor_procmac/src/lib.rs
Normal file
41
mission_rust/descriptor_procmac/src/lib.rs
Normal file
@ -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<core::ffi::c_int,()>{
|
||||
unsafe {
|
||||
let fd = #getter_name( );
|
||||
let written = write(fd, buf.as_ptr() as *const core::ffi::c_void, buf.len());
|
||||
Ok(written)
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
tokens.into()
|
||||
}
|
@ -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<core::ffi::c_int,()>;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user