Skip to content

Commit 7efef31

Browse files
committed
Convert 'gcoap.rs' to be heapless, get gcoap_get_v2() to work
1 parent 018cec3 commit 7efef31

File tree

4 files changed

+47
-50
lines changed

4 files changed

+47
-50
lines changed

examples/xbd-net/src/xbd/callback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use mcu_if::{alloc::boxed::Box, c_types::c_void};
2-
use super::gcoap::GcoapMemoState;
32
use super::stream::{XStream, XStreamData, StreamExt};
43
use crate::static_borrow_mut;
54

@@ -45,6 +44,7 @@ pub async fn process_api_stream() -> Result<(), i8> {
4544
},
4645
ApiCallback::_GcoapPing(_) => todo!(),
4746
// ApiCallback::GcoapReq(arg_ptr) => {
47+
// use super::gcoap::GcoapMemoState;
4848
// let (cb_ptr, out) = arg_from::<GcoapMemoState>(arg_ptr);
4949
// call(cb_ptr, out);
5050
// },

examples/xbd-net/src/xbd/gcoap.rs

+24-26
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
use core::{future::Future, pin::Pin, task::{Context, Poll}, cell::RefCell};
1+
use core::{future::Future, pin::Pin, task::{Context, Poll}};
22
use futures_util::task::AtomicWaker;
3-
use mcu_if::{alloc::{vec::Vec, rc::Rc}}; // !!!!
43
use super::{BlockwiseData, BLOCKWISE_HDR_MAX};
54

65
pub const REQ_ADDR_MAX: usize = 64;
76
pub const REQ_URI_MAX: usize = 64;
8-
const REQ_PAYLOAD_MAX: usize = 48;
9-
const REQ_OUT_MAX: usize = 128; // !!!!
7+
8+
const PAYLOAD_REQ_MAX: usize = 48;
9+
const PAYLOAD_OUT_MAX: usize = 128;
10+
11+
type PayloadReq = heapless::Vec<u8, PAYLOAD_REQ_MAX>;
12+
pub type PayloadOut = heapless::Vec<u8, PAYLOAD_OUT_MAX>;
1013

1114
//
1215
// gcoap client
@@ -27,14 +30,14 @@ const GCOAP_MEMO_RESP_TRUNC: u8 = 0x06;
2730

2831
#[derive(Debug, PartialEq)]
2932
pub enum GcoapMemoState {
30-
Resp(Option<Vec<u8>>),
33+
Resp(Option<PayloadOut>),
3134
Timeout,
3235
Err,
33-
RespTrunc(Option<Vec<u8>>),
36+
RespTrunc(Option<PayloadOut>),
3437
}
3538

3639
impl GcoapMemoState {
37-
pub fn new(memo_state: u8, payload: Option<Vec<u8>>) -> Self {
40+
pub fn new(memo_state: u8, payload: Option<PayloadOut>) -> Self {
3841
match memo_state {
3942
// ...
4043
GCOAP_MEMO_RESP => Self::Resp(payload),
@@ -80,7 +83,7 @@ pub enum Req {
8083

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

8689
match method {
@@ -107,25 +110,24 @@ impl Future for Req {
107110

108111
//
109112

110-
pub type Finale = (AtomicWaker, heapless::Vec<u8, REQ_OUT_MAX>);
113+
pub type Finale = (AtomicWaker, Option<GcoapMemoState>);
111114

112115
#[derive(Debug)]
113116
pub struct ReqInner {
114117
method: CoapMethod,
115118
addr: heapless::String<{ REQ_ADDR_MAX }>,
116119
uri: heapless::String<{ REQ_URI_MAX }>,
117-
payload: Option<heapless::Vec<u8, REQ_PAYLOAD_MAX>>,
120+
payload: Option<PayloadReq>,
118121
blockwise: bool,
119122
blockwise_state_index: Option<usize>,
120123
blockwise_hdr: Option<heapless::Vec<u8, BLOCKWISE_HDR_MAX>>,
121-
// out: Rc<RefCell<Option<GcoapMemoState>>>,
122124
_waker: Option<AtomicWaker>,
123-
finale: Option<Finale>,// !!!! !!!!
125+
finale: Option<Finale>,
124126
}
125127

126128
impl ReqInner {
127129
pub fn new(method: CoapMethod, addr: &str, uri: &str,
128-
payload: Option<heapless::Vec<u8, REQ_PAYLOAD_MAX>>,
130+
payload: Option<PayloadReq>,
129131
blockwise: bool,
130132
blockwise_state_index: Option<usize>,
131133
blockwise_hdr: Option<heapless::Vec<u8, BLOCKWISE_HDR_MAX>>) -> Self {
@@ -137,9 +139,8 @@ impl ReqInner {
137139
blockwise,
138140
blockwise_state_index,
139141
blockwise_hdr,
140-
// out: Rc::new(RefCell::new(None)),
141142
_waker: Some(AtomicWaker::new()),
142-
finale: None,// !!!! !!!!
143+
finale: None,
143144
}
144145
}
145146
}
@@ -156,7 +157,7 @@ impl Future for ReqInner {
156157
//outc.borrow_mut().replace(_out);
157158
//_waker.wake();
158159
//==== !!!!
159-
panic!("debug");
160+
panic!("BUILD SHIM");
160161
};
161162
match self.method {
162163
COAP_METHOD_GET => {
@@ -178,28 +179,25 @@ impl Future for ReqInner {
178179
}
179180
} else {
180181
//super::Xbd::gcoap_get(&self.addr, &self.uri, cb);
181-
//==== !!!! WIP
182-
self.finale.replace((_waker, heapless::Vec::new()));
182+
//==== !!!! v2
183+
self.finale.replace((_waker, None));
183184
super::Xbd::gcoap_get_v2(&self.addr, &self.uri,
184185
self.finale.as_ref().unwrap() as *const Finale as *mut _);
185186
}
186187
},
187-
COAP_METHOD_POST => super::Xbd::gcoap_post(
188+
COAP_METHOD_POST => super::Xbd::gcoap_post(// TODO -> v2
188189
&self.addr, &self.uri, self.payload.as_ref().unwrap().as_slice(), cb),
189-
COAP_METHOD_PUT => super::Xbd::gcoap_put(
190+
COAP_METHOD_PUT => super::Xbd::gcoap_put(// TODO -> v2
190191
&self.addr, &self.uri, self.payload.as_ref().unwrap().as_slice(), cb),
191192
_ => todo!(),
192193
}
193194

194195
Poll::Pending
195196
} else {
196197
//Poll::Ready(self.out.take().unwrap())
197-
//==== !!!!
198-
crate::println!("!!!! before Poll::Ready !!!! finale: {:?}", self.finale);
199-
let out = GcoapMemoState::new(// TODO heapless represent `out` from `gcoap_req_resp_handler`
200-
GCOAP_MEMO_RESP,
201-
Some(self.finale.as_ref().unwrap().1.as_slice().to_vec())); // dummy
202-
Poll::Ready(out)
198+
//==== v2
199+
let (_, out) = self.finale.take().unwrap();
200+
Poll::Ready(out.unwrap())
203201
}
204202
}
205203
}

examples/xbd-net/src/xbd/mod.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,28 @@ impl Xbd {
105105
}
106106
}
107107

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

112-
pub fn gcoap_get_v2(addr: &str, uri: &str, finale_ptr: *mut Finale) { // !!!!
113+
pub fn gcoap_get_v2(addr: &str, uri: &str, finale_ptr: *mut Finale) {
113114
Self::gcoap_req_v2(addr, uri, COAP_METHOD_GET, None, false, None, finale_ptr);
114115
}
115116

116117
pub fn gcoap_get_blockwise<F>(addr: &str, uri: &str, blockwise_state_index: usize, cb: F) where F: FnOnce(GcoapMemoState) + 'static {
117118
Self::gcoap_req(addr, uri, COAP_METHOD_GET, None, true, Some(blockwise_state_index), cb);
119+
// TODO -> v2
118120
}
119121

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

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

128132
fn gcoap_req<F>(addr: &str, uri: &str, method: gcoap::CoapMethod,
@@ -154,9 +158,10 @@ impl Xbd {
154158
Self::gcoap_req_resp_handler as *const c_void);
155159
}
156160
}
161+
157162
fn gcoap_req_v2(addr: &str, uri: &str, method: gcoap::CoapMethod,
158163
payload: Option<&[u8]>, blockwise: bool, blockwise_state_index: Option<usize>,
159-
finale_ptr: *mut Finale) { // !!!!
164+
finale_ptr: *mut Finale) {
160165
let payload_ptr = payload.map_or(core::ptr::null(), |payload| payload.as_ptr());
161166
let payload_len = payload.map_or(0, |payload| payload.len());
162167

@@ -172,29 +177,23 @@ impl Xbd {
172177
*const u8, *const u8, u8,
173178
*const u8, usize, bool, usize, *const c_void, *const c_void);
174179

175-
if 0 == 1 {// waypoint, ok
176-
let (waker, hv) = unsafe { &mut *finale_ptr };
177-
hv.push(42).unwrap();
178-
hv.push(42+1).unwrap();
179-
hv.push(42+2).unwrap();
180-
waker.wake();
181-
return;
182-
}
183-
184180
assert_eq!(blockwise, blockwise_state_index.is_some());
185181
unsafe {
186182
(get_xbd_fn!("xbd_gcoap_req_send", Ty))(
187183
addr_cstr.as_ptr(),
188184
uri_cstr.as_ptr(),
189185
method, payload_ptr, payload_len,
190186
blockwise, blockwise_state_index.unwrap_or(0 /* to be ignored */),
191-
//callback::into_raw(cb), // context !!!!
192-
finale_ptr as *const c_void, // context !!!! WIP
193-
Self::gcoap_req_resp_handler as *const c_void);
187+
finale_ptr as *const c_void, // context
188+
Self::gcoap_req_resp_handler_v2 as *const c_void);
194189
}
195190
}
196191

197-
fn gcoap_req_resp_handler(memo: *const c_void, pdu: *const c_void, remote: *const c_void) {
192+
fn gcoap_req_resp_handler(_memo: *const c_void, _pdu: *const c_void, _remote: *const c_void) {
193+
panic!("BUILD SHIM");
194+
}
195+
196+
fn gcoap_req_resp_handler_v2(memo: *const c_void, pdu: *const c_void, remote: *const c_void) {
198197
let mut context: *const c_void = core::ptr::null_mut();
199198
let mut payload_ptr: *const u8 = core::ptr::null_mut();
200199
let mut payload_len: usize = 0;
@@ -207,20 +206,20 @@ impl Xbd {
207206
(&mut context) as *mut *const c_void as *mut c_void) };
208207

209208
let payload = if payload_len > 0 {
210-
Some(u8_slice_from(payload_ptr, payload_len).to_vec()) // !!!!
209+
let hvec: gcoap::PayloadOut = heapless::Vec::from_slice(
210+
u8_slice_from(payload_ptr, payload_len)).unwrap();
211+
Some(hvec)
211212
} else {
212213
assert_eq!(payload_ptr, core::ptr::null_mut());
213214
None
214215
};
215-
let out = GcoapMemoState::new(memo_state, payload);
216+
let memo = GcoapMemoState::new(memo_state, payload);
216217

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

examples/xbd-net/src/xbd/shell.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ const ARRAY_ALIAS_FUNCTION: &[&str] = &[
171171

172172
async fn run_function_alias(name: &str) {
173173
match name {
174-
"f" => test_heapless_req().await,
174+
"f" => test_heapless_req().await, // !!!!
175175
"f0" => (|| println!("hello world!"))(),
176176
"f1" => test_async_sleep().await,
177177
"f2" => test_async_blockwise_get().await,

0 commit comments

Comments
 (0)