Skip to content

Commit

Permalink
Convert 'gcoap.rs' to be heapless, get gcoap_get_v2() to work
Browse files Browse the repository at this point in the history
  • Loading branch information
j-devel committed Jun 12, 2024
1 parent 018cec3 commit 7efef31
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 50 deletions.
2 changes: 1 addition & 1 deletion examples/xbd-net/src/xbd/callback.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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;

Expand Down Expand Up @@ -45,6 +44,7 @@ pub async fn process_api_stream() -> Result<(), i8> {
},
ApiCallback::_GcoapPing(_) => todo!(),
// ApiCallback::GcoapReq(arg_ptr) => {
// use super::gcoap::GcoapMemoState;
// let (cb_ptr, out) = arg_from::<GcoapMemoState>(arg_ptr);
// call(cb_ptr, out);
// },
Expand Down
50 changes: 24 additions & 26 deletions examples/xbd-net/src/xbd/gcoap.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use core::{future::Future, pin::Pin, task::{Context, Poll}, cell::RefCell};
use core::{future::Future, pin::Pin, task::{Context, Poll}};
use futures_util::task::AtomicWaker;
use mcu_if::{alloc::{vec::Vec, rc::Rc}}; // !!!!
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; // !!!!

const PAYLOAD_REQ_MAX: usize = 48;
const PAYLOAD_OUT_MAX: usize = 128;

type PayloadReq = heapless::Vec<u8, PAYLOAD_REQ_MAX>;
pub type PayloadOut = heapless::Vec<u8, PAYLOAD_OUT_MAX>;

//
// gcoap client
Expand All @@ -27,14 +30,14 @@ const GCOAP_MEMO_RESP_TRUNC: u8 = 0x06;

#[derive(Debug, PartialEq)]
pub enum GcoapMemoState {
Resp(Option<Vec<u8>>),
Resp(Option<PayloadOut>),
Timeout,
Err,
RespTrunc(Option<Vec<u8>>),
RespTrunc(Option<PayloadOut>),
}

impl GcoapMemoState {
pub fn new(memo_state: u8, payload: Option<Vec<u8>>) -> Self {
pub fn new(memo_state: u8, payload: Option<PayloadOut>) -> Self {
match memo_state {
// ...
GCOAP_MEMO_RESP => Self::Resp(payload),
Expand Down Expand Up @@ -80,7 +83,7 @@ pub enum Req {

impl Req {
pub fn new(method: CoapMethod, addr: &str, uri: &str,
payload: Option<heapless::Vec<u8, REQ_PAYLOAD_MAX>>) -> Self {
payload: Option<PayloadReq>) -> Self {
let inner = ReqInner::new(method, addr, uri, payload, false, None, None);

match method {
Expand All @@ -107,25 +110,24 @@ impl Future for Req {

//

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

#[derive(Debug)]
pub struct ReqInner {
method: CoapMethod,
addr: heapless::String<{ REQ_ADDR_MAX }>,
uri: heapless::String<{ REQ_URI_MAX }>,
payload: Option<heapless::Vec<u8, REQ_PAYLOAD_MAX>>,
payload: Option<PayloadReq>,
blockwise: bool,
blockwise_state_index: Option<usize>,
blockwise_hdr: Option<heapless::Vec<u8, BLOCKWISE_HDR_MAX>>,
// out: Rc<RefCell<Option<GcoapMemoState>>>,
_waker: Option<AtomicWaker>,
finale: Option<Finale>,// !!!! !!!!
finale: Option<Finale>,
}

impl ReqInner {
pub fn new(method: CoapMethod, addr: &str, uri: &str,
payload: Option<heapless::Vec<u8, REQ_PAYLOAD_MAX>>,
payload: Option<PayloadReq>,
blockwise: bool,
blockwise_state_index: Option<usize>,
blockwise_hdr: Option<heapless::Vec<u8, BLOCKWISE_HDR_MAX>>) -> Self {
Expand All @@ -137,9 +139,8 @@ impl ReqInner {
blockwise,
blockwise_state_index,
blockwise_hdr,
// out: Rc::new(RefCell::new(None)),
_waker: Some(AtomicWaker::new()),
finale: None,// !!!! !!!!
finale: None,
}
}
}
Expand All @@ -156,7 +157,7 @@ impl Future for ReqInner {
//outc.borrow_mut().replace(_out);
//_waker.wake();
//==== !!!!
panic!("debug");
panic!("BUILD SHIM");
};
match self.method {
COAP_METHOD_GET => {
Expand All @@ -178,28 +179,25 @@ impl Future for ReqInner {
}
} else {
//super::Xbd::gcoap_get(&self.addr, &self.uri, cb);
//==== !!!! WIP
self.finale.replace((_waker, heapless::Vec::new()));
//==== !!!! v2
self.finale.replace((_waker, None));
super::Xbd::gcoap_get_v2(&self.addr, &self.uri,
self.finale.as_ref().unwrap() as *const Finale as *mut _);
}
},
COAP_METHOD_POST => super::Xbd::gcoap_post(
COAP_METHOD_POST => super::Xbd::gcoap_post(// TODO -> v2
&self.addr, &self.uri, self.payload.as_ref().unwrap().as_slice(), cb),
COAP_METHOD_PUT => super::Xbd::gcoap_put(
COAP_METHOD_PUT => super::Xbd::gcoap_put(// TODO -> v2
&self.addr, &self.uri, self.payload.as_ref().unwrap().as_slice(), cb),
_ => todo!(),
}

Poll::Pending
} else {
//Poll::Ready(self.out.take().unwrap())
//==== !!!!
crate::println!("!!!! before Poll::Ready !!!! finale: {:?}", self.finale);
let out = GcoapMemoState::new(// TODO heapless represent `out` from `gcoap_req_resp_handler`
GCOAP_MEMO_RESP,
Some(self.finale.as_ref().unwrap().1.as_slice().to_vec())); // dummy
Poll::Ready(out)
//==== v2
let (_, out) = self.finale.take().unwrap();
Poll::Ready(out.unwrap())
}
}
}
Expand Down
43 changes: 21 additions & 22 deletions examples/xbd-net/src/xbd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,28 @@ impl Xbd {
}
}

// todo REMOVE
pub fn gcoap_get<F>(addr: &str, uri: &str, cb: F) where F: FnOnce(GcoapMemoState) + 'static {
Self::gcoap_req(addr, uri, COAP_METHOD_GET, None, false, None, cb);
}

pub fn gcoap_get_v2(addr: &str, uri: &str, finale_ptr: *mut Finale) { // !!!!
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);
// TODO -> v2
}

pub fn gcoap_post<F>(addr: &str, uri: &str, payload: &[u8], cb: F) where F: FnOnce(GcoapMemoState) + 'static {
Self::gcoap_req(addr, uri, gcoap::COAP_METHOD_POST, Some(payload), false, None, cb);
// TODO -> v2
}

pub fn gcoap_put<F>(addr: &str, uri: &str, payload: &[u8], cb: F) where F: FnOnce(GcoapMemoState) + 'static {
Self::gcoap_req(addr, uri, gcoap::COAP_METHOD_PUT, Some(payload), false, None, cb);
// TODO -> v2
}

fn gcoap_req<F>(addr: &str, uri: &str, method: gcoap::CoapMethod,
Expand Down Expand Up @@ -154,9 +158,10 @@ 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>,
finale_ptr: *mut Finale) { // !!!!
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());

Expand All @@ -172,29 +177,23 @@ impl Xbd {
*const u8, *const u8, u8,
*const u8, usize, bool, usize, *const c_void, *const c_void);

if 0 == 1 {// waypoint, ok
let (waker, hv) = unsafe { &mut *finale_ptr };
hv.push(42).unwrap();
hv.push(42+1).unwrap();
hv.push(42+2).unwrap();
waker.wake();
return;
}

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 !!!!
finale_ptr as *const c_void, // context !!!! WIP
Self::gcoap_req_resp_handler as *const c_void);
finale_ptr as *const c_void, // context
Self::gcoap_req_resp_handler_v2 as *const c_void);
}
}

fn gcoap_req_resp_handler(memo: *const c_void, pdu: *const c_void, remote: *const c_void) {
fn gcoap_req_resp_handler(_memo: *const c_void, _pdu: *const c_void, _remote: *const c_void) {
panic!("BUILD SHIM");
}

fn gcoap_req_resp_handler_v2(memo: *const c_void, pdu: *const c_void, remote: *const c_void) {
let mut context: *const c_void = core::ptr::null_mut();
let mut payload_ptr: *const u8 = core::ptr::null_mut();
let mut payload_len: usize = 0;
Expand All @@ -207,20 +206,20 @@ 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()) // !!!!
let hvec: gcoap::PayloadOut = heapless::Vec::from_slice(
u8_slice_from(payload_ptr, payload_len)).unwrap();
Some(hvec)
} else {
assert_eq!(payload_ptr, core::ptr::null_mut());
None
};
let out = GcoapMemoState::new(memo_state, payload);
let memo = GcoapMemoState::new(memo_state, payload);

// add_xbd_gcoap_req_callback(
// Box::into_raw(Box::new((context /* cb_ptr */, out))) as *const c_void); // arg_ptr
//==== !!!! TODO hv <<<< out, heepless--ly
let (waker, hv) = unsafe { &mut *(context as *mut Finale) };
hv.push(42).unwrap();
hv.push(42+1).unwrap();
hv.push(42+2).unwrap();
//==== !!!! v2
let (waker, out) = unsafe { &mut *(context as *mut Finale) };
out.replace(memo);
waker.wake();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/xbd-net/src/xbd/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const ARRAY_ALIAS_FUNCTION: &[&str] = &[

async fn run_function_alias(name: &str) {
match name {
"f" => test_heapless_req().await,
"f" => test_heapless_req().await, // !!!!
"f0" => (|| println!("hello world!"))(),
"f1" => test_async_sleep().await,
"f2" => test_async_blockwise_get().await,
Expand Down

0 comments on commit 7efef31

Please sign in to comment.