Skip to content

Commit

Permalink
Backport 'util.rs' from rust-async-wip, fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
j-devel committed Jun 5, 2024
1 parent 8fb3dcb commit 8626777
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 26 deletions.
3 changes: 2 additions & 1 deletion examples/xbd-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
11 changes: 6 additions & 5 deletions examples/xbd-net/src/embassy/executor.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -48,17 +48,18 @@ 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 {
unsafe { self.inner.poll() };
}
}
}
}
}
12 changes: 1 addition & 11 deletions examples/xbd-net/src/embassy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,11 @@ async fn task_shell_stream() {
xbd::process_shell_stream().await.unwrap();
}

fn to_raw<T>(x: &mut T) -> *mut () { x as *mut _ as _ }
fn static_from_raw<T>(p: *mut ()) -> &'static mut T { unsafe { core::mem::transmute(p) } }
pub fn get_static<T>(x: &mut T) -> &'static mut T { static_from_raw(to_raw(x)) }
/* deprecated
pub fn get_static<T>(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) -> ! {
Expand Down
16 changes: 11 additions & 5 deletions examples/xbd-net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +18,15 @@ use xbd::{Xbd, XbdFnsEnt, GcoapMemoState};
mod blogos12;
mod embassy;

mod util;
use util::get_static;
/* deprecated
fn get_static<T>(x: &mut T) -> &'static mut T {
use mcu_if::alloc::boxed::Box;
Box::leak(Box::new(x))
}
*/

//

#[no_mangle]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -345,4 +351,4 @@ $ libcoap/local/bin/coap-client -m get coaps://[::1]/.well-known/core -k "secret
//

Ok(())
}
}
47 changes: 47 additions & 0 deletions examples/xbd-net/src/util.rs
Original file line number Diff line number Diff line change
@@ -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<T>(x: &mut T) -> *mut () { x as *mut _ as _ }
pub fn static_from_raw<T>(p: *mut ()) -> &'static mut T { unsafe { core::mem::transmute(p) } }
pub fn get_static<T>(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<<Self as Future>::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.");
}
}
}
*/
3 changes: 2 additions & 1 deletion examples/xbd-net/src/xbd/blockwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -280,4 +281,4 @@ impl Stream for BlockwiseStream {
}
}
}
}
}
3 changes: 2 additions & 1 deletion examples/xbd-net/src/xbd/callback.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -72,4 +73,4 @@ fn cb_from<T>(cb_ptr: CVoidPtr) -> Box<Box<dyn FnOnce(T) + 'static>> {
fn call<T>(cb_ptr: CVoidPtr, out: T) {
assert_ne!(cb_ptr, core::ptr::null());
(*(cb_from(cb_ptr)))(out); // call, move, drop
}
}
3 changes: 2 additions & 1 deletion examples/xbd-net/src/xbd/server.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -141,4 +142,4 @@ pub extern fn xbd_riot_fileserver_handler(
println!("@@ xbd_riot_fileserver_handler(): pdu_len: {:?}", pdu_len);

pdu_len
}
}
3 changes: 2 additions & 1 deletion examples/xbd-net/src/xbd/shell.rs
Original file line number Diff line number Diff line change
@@ -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 ();
Expand Down Expand Up @@ -211,4 +212,4 @@ async fn match_alias(line: &mut ShellBuf) -> bool {
}

false
}
}

0 comments on commit 8626777

Please sign in to comment.