#![allow(unknown_lints)]
#![allow(unexpected_cfgs)]
use core::ffi::c_void;
use std::ffi::CString;
use esp_idf_sys::{restore_posix_stdio_fds, write, STDERR_FILENO, STDOUT_FILENO};
fn main() {
cprintln("--- POSIX stdio descriptor check ---");
let bound = restore_posix_stdio_fds();
cprintln(&format!("restore_posix_stdio_fds() -> {}", bound as u8));
let out = raw_write(
STDOUT_FILENO as _,
"raw write to descriptor 1 (Rust stdout)\r\n",
);
let err = raw_write(
STDERR_FILENO as _,
"raw write to descriptor 2 (Rust stderr)\r\n",
);
cprintln(&format!(
"raw write to descriptor 1 (Rust stdout) returned {out}"
));
cprintln(&format!(
"raw write to descriptor 2 (Rust stderr) returned {err}"
));
println!("Rust println! works");
eprintln!("Rust eprintln! works");
cprintln("--- done ---");
}
fn cprintln(msg: &str) {
let msg = CString::new(msg).unwrap();
unsafe {
esp_idf_sys::printf(c"%s\n".as_ptr(), msg.as_ptr());
}
}
fn raw_write(fd: i32, msg: &str) -> i64 {
unsafe { write(fd, msg.as_ptr() as *const c_void, msg.len()) as i64 }
}