From 8626777a914e9a3d1177ae40944fb527fbc10666 Mon Sep 17 00:00:00 2001 From: j-devel <38098238+j-devel@users.noreply.github.com> Date: Wed, 5 Jun 2024 17:41:57 +0700 Subject: [PATCH] Backport 'util.rs' from rust-async-wip, fix build --- examples/xbd-net/Cargo.toml | 3 +- examples/xbd-net/src/embassy/executor.rs | 11 +++--- examples/xbd-net/src/embassy/mod.rs | 12 +----- examples/xbd-net/src/lib.rs | 16 +++++--- examples/xbd-net/src/util.rs | 47 ++++++++++++++++++++++++ examples/xbd-net/src/xbd/blockwise.rs | 3 +- examples/xbd-net/src/xbd/callback.rs | 3 +- examples/xbd-net/src/xbd/server.rs | 3 +- examples/xbd-net/src/xbd/shell.rs | 3 +- 9 files changed, 75 insertions(+), 26 deletions(-) create mode 100644 examples/xbd-net/src/util.rs diff --git a/examples/xbd-net/Cargo.toml b/examples/xbd-net/Cargo.toml index bb36529..939b506 100755 --- a/examples/xbd-net/Cargo.toml +++ b/examples/xbd-net/Cargo.toml @@ -15,4 +15,5 @@ crossbeam-queue = { version = "0.3.8", default-features = false, features=["allo conquer-once = { version = "0.4.0", default-features = false } futures-util = { version = "0.3.4", default-features = false } embassy-executor = { path = "crates/embassy/embassy-executor", default-features = false, features=["nightly"] } -heapless = { version = "0.8.0" } +heapless = "0.8" +##TODO##riot-wrappers = { version = "0.8", features = [ "set_panic_handler" ] } diff --git a/examples/xbd-net/src/embassy/executor.rs b/examples/xbd-net/src/embassy/executor.rs index f8d8e89..95d1738 100644 --- a/examples/xbd-net/src/embassy/executor.rs +++ b/examples/xbd-net/src/embassy/executor.rs @@ -1,4 +1,4 @@ -use riot_wrappers::{println, ztimer}; +//use riot_wrappers::{println, ztimer}; use embassy_executor::{Spawner, raw}; use conquer_once::spin::OnceCell; use crate::util::static_from_raw; @@ -48,12 +48,13 @@ impl Executor { assert_eq!(self.signaler.dummy, SIGNALER_DUMMY_DATA); if let Some(ms) = self.throttle { - println!("Executor::run(): throttle: {:?} ms", ms); - let timer = ztimer::Clock::msec(); + crate::println!("Executor::run(): throttle: {:?} ms", ms); +// let timer = ztimer::Clock::msec(); loop { unsafe { self.inner.poll() }; - timer.sleep_ticks(ms); +// timer.sleep_ticks(ms); + crate::Xbd::msleep(ms, false); } } else { loop { @@ -61,4 +62,4 @@ impl Executor { } } } -} \ No newline at end of file +} diff --git a/examples/xbd-net/src/embassy/mod.rs b/examples/xbd-net/src/embassy/mod.rs index a54a3f5..5c55cc3 100644 --- a/examples/xbd-net/src/embassy/mod.rs +++ b/examples/xbd-net/src/embassy/mod.rs @@ -23,21 +23,11 @@ async fn task_shell_stream() { xbd::process_shell_stream().await.unwrap(); } -fn to_raw(x: &mut T) -> *mut () { x as *mut _ as _ } -fn static_from_raw(p: *mut ()) -> &'static mut T { unsafe { core::mem::transmute(p) } } -pub fn get_static(x: &mut T) -> &'static mut T { static_from_raw(to_raw(x)) } -/* deprecated -pub fn get_static(x: &mut T) -> &'static mut T { - use mcu_if::alloc::boxed::Box; - Box::leak(Box::new(x)) -} -*/ - pub struct Runtime(Executor); impl Runtime { pub fn new() -> Self { - Self(Executor::new()) + Self(Executor::new(Some(100))) } pub fn run(&'static mut self) -> ! { diff --git a/examples/xbd-net/src/lib.rs b/examples/xbd-net/src/lib.rs index f32954e..59d9d92 100644 --- a/examples/xbd-net/src/lib.rs +++ b/examples/xbd-net/src/lib.rs @@ -10,9 +10,6 @@ fn panic(info: &core::panic::PanicInfo) -> ! { mcu_if::panic(info) } #[alloc_error_handler] fn alloc_error(layout: mcu_if::alloc::alloc::Layout) -> ! { mcu_if::alloc_error(layout) } -#[macro_export] -macro_rules! static_borrow_mut { ($x:expr) => (unsafe { &mut *core::ptr::addr_of_mut!($x) }) } - use mcu_if::{println, alloc::boxed::Box}; mod xbd; @@ -21,6 +18,15 @@ use xbd::{Xbd, XbdFnsEnt, GcoapMemoState}; mod blogos12; mod embassy; +mod util; +use util::get_static; +/* deprecated +fn get_static(x: &mut T) -> &'static mut T { + use mcu_if::alloc::boxed::Box; + Box::leak(Box::new(x)) +} +*/ + // #[no_mangle] @@ -55,7 +61,7 @@ pub extern fn rustmod_start( if 1 == 1 { println!("@@ [debug] `xbd_main()` with `embassy::Runtime` ..."); - embassy::get_static(&mut embassy::Runtime::new()) + get_static(&mut embassy::Runtime::new()) .run(); // -> ! // should be never reached @@ -345,4 +351,4 @@ $ libcoap/local/bin/coap-client -m get coaps://[::1]/.well-known/core -k "secret // Ok(()) -} \ No newline at end of file +} diff --git a/examples/xbd-net/src/util.rs b/examples/xbd-net/src/util.rs new file mode 100644 index 0000000..ccbafaf --- /dev/null +++ b/examples/xbd-net/src/util.rs @@ -0,0 +1,47 @@ +#[macro_export] +macro_rules! static_borrow_mut { ($x:expr) => (unsafe { &mut *core::ptr::addr_of_mut!($x) }) } + +pub fn to_raw(x: &mut T) -> *mut () { x as *mut _ as _ } +pub fn static_from_raw(p: *mut ()) -> &'static mut T { unsafe { core::mem::transmute(p) } } +pub fn get_static(x: &mut T) -> &'static mut T { static_from_raw(to_raw(x)) } +/* +pub async fn sleep_msec(ms: u32) { + use riot_wrappers::ztimer::*; + + Clock::msec().sleep_async(Ticks(ms)).await; +} + +pub async fn set_timeout(msec: u32, cb: impl FnOnce()) { + sleep_msec(msec).await; + cb(); +} + +use core::{future::Future, pin::Pin, task::{Context, Poll}}; +struct Forever; +impl Future for Forever { + type Output = (); + fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<::Output> { + Poll::Pending + } +} + +pub async fn sleep_forever() { + (Forever {}).await; + panic!("should be never reached"); +} + +pub fn announce_netif() { + use riot_wrappers::{gnrc, println}; + + println!("announce_netif():"); + for netif in gnrc::Netif::all() { + println!("active interface from PID {:?} ({:?})", + netif.pid(), netif.pid().get_name().unwrap_or("unnamed")); + if let Ok(addrs) = netif.ipv6_addrs() { + addrs.iter().for_each(|a| println!(" Address {:?}", a)); + } else { + println!(" Does not support IPv6."); + } + } +} +*/ diff --git a/examples/xbd-net/src/xbd/blockwise.rs b/examples/xbd-net/src/xbd/blockwise.rs index af93042..560e4b0 100644 --- a/examples/xbd-net/src/xbd/blockwise.rs +++ b/examples/xbd-net/src/xbd/blockwise.rs @@ -4,6 +4,7 @@ use core::{str::from_utf8, pin::Pin, task::{Context, Poll}}; use futures_util::stream::Stream; use super::stream::{XStream, XStreamData}; use super::gcoap::{ReqInner, COAP_METHOD_GET, REQ_ADDR_MAX, REQ_URI_MAX}; +use crate::static_borrow_mut; #[no_mangle] pub extern fn xbd_blockwise_state_index() -> usize { @@ -280,4 +281,4 @@ impl Stream for BlockwiseStream { } } } -} \ No newline at end of file +} diff --git a/examples/xbd-net/src/xbd/callback.rs b/examples/xbd-net/src/xbd/callback.rs index 18c950b..2838514 100644 --- a/examples/xbd-net/src/xbd/callback.rs +++ b/examples/xbd-net/src/xbd/callback.rs @@ -1,6 +1,7 @@ use mcu_if::{alloc::boxed::Box, c_types::c_void}; use super::gcoap::GcoapMemoState; use super::stream::{XStream, XStreamData, StreamExt}; +use crate::static_borrow_mut; extern "C" { fn free(ptr: *mut c_void); @@ -72,4 +73,4 @@ fn cb_from(cb_ptr: CVoidPtr) -> Box> { fn call(cb_ptr: CVoidPtr, out: T) { assert_ne!(cb_ptr, core::ptr::null()); (*(cb_from(cb_ptr)))(out); // call, move, drop -} \ No newline at end of file +} diff --git a/examples/xbd-net/src/xbd/server.rs b/examples/xbd-net/src/xbd/server.rs index 398e817..4491ab1 100644 --- a/examples/xbd-net/src/xbd/server.rs +++ b/examples/xbd-net/src/xbd/server.rs @@ -1,6 +1,7 @@ use mcu_if::{println, alloc::boxed::Box, c_types::c_void}; use super::callback::{Ptr32Send, arg_from}; use super::stream::{XStream, XStreamData, StreamExt}; +use crate::static_borrow_mut; extern "C" { fn server_init() -> i8; @@ -141,4 +142,4 @@ pub extern fn xbd_riot_fileserver_handler( println!("@@ xbd_riot_fileserver_handler(): pdu_len: {:?}", pdu_len); pdu_len -} \ No newline at end of file +} diff --git a/examples/xbd-net/src/xbd/shell.rs b/examples/xbd-net/src/xbd/shell.rs index 14e98dd..5dc4c2e 100644 --- a/examples/xbd-net/src/xbd/shell.rs +++ b/examples/xbd-net/src/xbd/shell.rs @@ -1,5 +1,6 @@ use super::stream::{XStream, XStreamData, StreamExt}; use crate::println; +use crate::static_borrow_mut; extern "C" { fn xbd_shell_get_commands() -> *const (); @@ -211,4 +212,4 @@ async fn match_alias(line: &mut ShellBuf) -> bool { } false -} \ No newline at end of file +}