Skip to content

Commit

Permalink
Add gcoap_get_v2(), return Poll::Ready for ReqInner via heaples…
Browse files Browse the repository at this point in the history
…s `Finale`
  • Loading branch information
j-devel committed Jun 11, 2024
1 parent dc0b88e commit 9067859
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
25 changes: 23 additions & 2 deletions examples/xbd-net/src/xbd/gcoap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::{BlockwiseData, BLOCKWISE_HDR_MAX};
pub const REQ_ADDR_MAX: usize = 64;
pub const REQ_URI_MAX: usize = 64;
const REQ_PAYLOAD_MAX: usize = 48;
const REQ_OUT_MAX: usize = 128; // !!!!

//
// gcoap client
Expand Down Expand Up @@ -106,6 +107,8 @@ impl Future for Req {

//

pub type Finale = (AtomicWaker, heapless::Vec<u8, REQ_OUT_MAX>);

#[derive(Debug)]
pub struct ReqInner {
method: CoapMethod,
Expand All @@ -117,6 +120,7 @@ pub struct ReqInner {
blockwise_hdr: Option<heapless::Vec<u8, BLOCKWISE_HDR_MAX>>,
out: Rc<RefCell<Option<GcoapMemoState>>>,
_waker: Option<AtomicWaker>,
finale: Option<Finale>,// !!!! !!!!
}

impl ReqInner {
Expand All @@ -135,6 +139,7 @@ impl ReqInner {
blockwise_hdr,
out: Rc::new(RefCell::new(None)),
_waker: Some(AtomicWaker::new()),
finale: None,// !!!! !!!!
}
}
}
Expand All @@ -146,10 +151,16 @@ impl Future for ReqInner {
if let Some(_waker) = self._waker.take() {
_waker.register(&cx.waker());

let outc = self.out.clone();
/*let outc = self.out.clone();
let cb = move |out| {
outc.borrow_mut().replace(out);
_waker.wake();
};*/
let cb = |_out| {
//outc.borrow_mut().replace(out);
//_waker.wake();
//==== !!!!
panic!("mmm");
};
match self.method {
COAP_METHOD_GET => {
Expand All @@ -170,7 +181,15 @@ impl Future for ReqInner {
return Poll::Ready(GcoapMemoState::Err)
}
} else {
super::Xbd::gcoap_get(&self.addr, &self.uri, cb);
//super::Xbd::gcoap_get(&self.addr, &self.uri, cb);
//==== !!!!
// use mcu_if::c_types::c_void;
self.finale.replace((_waker, heapless::Vec::new()));
super::Xbd::gcoap_get_v2(&self.addr, &self.uri,
// /* move */_waker); // !!!!
// self._waker.as_ref() as *const _); // !!!!
// self.finale.as_ref().unwrap() as *const _ as *const c_void); // !!!! `&(AtomicWaker, heapless::Vec<u8, 2>)`
self.finale.as_ref().unwrap() as *const Finale as *mut _); // !!!!
}
},
COAP_METHOD_POST => super::Xbd::gcoap_post(
Expand All @@ -182,6 +201,8 @@ impl Future for ReqInner {

Poll::Pending
} else {
crate::println!("!!!! before Poll::Ready !!!! finale: {:?}", self.finale);

Poll::Ready(self.out.take().unwrap())
}
}
Expand Down
57 changes: 55 additions & 2 deletions examples/xbd-net/src/xbd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod timeout;
use timeout::Timeout;

mod gcoap;
use gcoap::{COAP_METHOD_GET, REQ_ADDR_MAX, REQ_URI_MAX};
use gcoap::{COAP_METHOD_GET, REQ_ADDR_MAX, REQ_URI_MAX, Finale};
pub use gcoap::GcoapMemoState;

use core::future::Future;
Expand Down Expand Up @@ -105,8 +105,15 @@ impl Xbd {
}

pub fn gcoap_get<F>(addr: &str, uri: &str, cb: F) where F: FnOnce(GcoapMemoState) + 'static {
// pub fn gcoap_get(addr: &str, uri: &str, cb: fn(GcoapMemoState)) { // !!!!
Self::gcoap_req(addr, uri, COAP_METHOD_GET, None, false, None, cb);
}
// pub fn gcoap_get_v2(addr: &str, uri: &str, waker: futures_util::task::AtomicWaker) { // !!!!
// pub fn gcoap_get_v2(addr: &str, uri: &str, waker_ptr: *const futures_util::task::AtomicWaker) { // !!!!
// pub fn gcoap_get_v2(addr: &str, uri: &str, finale_ptr: *const c_void) { // !!!!
pub fn gcoap_get_v2(addr: &str, uri: &str, finale_ptr: *mut Finale) { // !!!!
Self::gcoap_req_v2(addr, uri, COAP_METHOD_GET, None, false, None, finale_ptr);
}

pub fn gcoap_get_blockwise<F>(addr: &str, uri: &str, blockwise_state_index: usize, cb: F) where F: FnOnce(GcoapMemoState) + 'static {
Self::gcoap_req(addr, uri, COAP_METHOD_GET, None, true, Some(blockwise_state_index), cb);
Expand Down Expand Up @@ -149,6 +156,52 @@ impl Xbd {
Self::gcoap_req_resp_handler as *const c_void);
}
}
fn gcoap_req_v2(addr: &str, uri: &str, method: gcoap::CoapMethod,
payload: Option<&[u8]>, blockwise: bool, blockwise_state_index: Option<usize>,
// waker: futures_util::task::AtomicWaker) { // !!!!
// waker_ptr: *const futures_util::task::AtomicWaker) { // !!!!
// finale_ptr: *const c_void) { // !!!!
finale_ptr: *mut Finale) { // !!!!
let payload_ptr = payload.map_or(core::ptr::null(), |payload| payload.as_ptr());
let payload_len = payload.map_or(0, |payload| payload.len());

let mut addr_cstr = heapless::String::<{ REQ_ADDR_MAX + 1 }>::new();
addr_cstr.push_str(addr).unwrap();
addr_cstr.push('\0').unwrap();

let mut uri_cstr = heapless::String::<{ REQ_URI_MAX + 1 }>::new();
uri_cstr.push_str(uri).unwrap();
uri_cstr.push('\0').unwrap();

type Ty = unsafe extern "C" fn(
*const u8, *const u8, u8,
*const u8, usize, bool, usize, *const c_void, *const c_void);

if 1 == 1 {// ok
// use futures_util::task::AtomicWaker;

let (waker, vec) = unsafe {
&mut *(finale_ptr as *mut Finale) };
vec.push(42).unwrap();
vec.push(42+1).unwrap();
vec.push(42+2).unwrap();
waker.wake();
}
if 0 == 1 {
assert_eq!(blockwise, blockwise_state_index.is_some());
unsafe {
(get_xbd_fn!("xbd_gcoap_req_send", Ty))(
addr_cstr.as_ptr(),
uri_cstr.as_ptr(),
method, payload_ptr, payload_len,
blockwise, blockwise_state_index.unwrap_or(0 /* to be ignored */),
// callback::into_raw(cb), // context !!!!!!!!! push `waker` how??????????
// waker_ptr as *const c_void, // !!!!
finale_ptr as *const c_void, // !!!!
Self::gcoap_req_resp_handler as *const c_void);
}
}
}

fn gcoap_req_resp_handler(memo: *const c_void, pdu: *const c_void, remote: *const c_void) {
let mut context: *const c_void = core::ptr::null_mut();
Expand All @@ -163,7 +216,7 @@ impl Xbd {
(&mut context) as *mut *const c_void as *mut c_void) };

let payload = if payload_len > 0 {
Some(u8_slice_from(payload_ptr, payload_len).to_vec())
Some(u8_slice_from(payload_ptr, payload_len).to_vec()) // !!!!
} else {
assert_eq!(payload_ptr, core::ptr::null_mut());
None
Expand Down

0 comments on commit 9067859

Please sign in to comment.