Skip to content

Commit 4801dc4

Browse files
committed
blockwise.rs: Rewrite with stream::{XStream, XStreamData}
1 parent cda27c1 commit 4801dc4

File tree

1 file changed

+19
-35
lines changed

1 file changed

+19
-35
lines changed

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

+19-35
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use mcu_if::c_types::c_void;
22
use mcu_if::utils::{u8_slice_from, u8_slice_mut_from};
33
use core::{str::from_utf8, pin::Pin, task::{Context, Poll}};
44
use futures_util::stream::Stream;
5-
use super::stream::{XbdStream, StreamData, stream_uninit};
5+
use super::stream::{XStream, XStreamData};
66
use super::gcoap::{ReqInner, COAP_METHOD_GET, REQ_ADDR_MAX, REQ_URI_MAX};
77

88
#[no_mangle]
@@ -76,8 +76,8 @@ static mut GRID_URI: &'static mut GridUri = &mut [[0; REQ_URI_MAX]; BLOCKWISE_ST
7676
type GridHdr = [(usize, [u8; BLOCKWISE_HDR_MAX]); BLOCKWISE_STATES_MAX];
7777
static mut GRID_HDR: &'static mut GridHdr = &mut [(0, [0; BLOCKWISE_HDR_MAX]); BLOCKWISE_STATES_MAX];
7878

79-
type BlockwiseStreamData = StreamData<Option<ReqInner>>;
80-
const ARRAY_REPEAT_VALUE_SD: BlockwiseStreamData = stream_uninit();
79+
type BlockwiseStreamData = XStreamData<Option<ReqInner>, 2>;
80+
const ARRAY_REPEAT_VALUE_SD: BlockwiseStreamData = XStream::init();
8181
static mut BLOCKWISE_SD: &'static mut [BlockwiseStreamData; BLOCKWISE_STATES_MAX] =
8282
&mut [ARRAY_REPEAT_VALUE_SD; BLOCKWISE_STATES_MAX];
8383

@@ -156,7 +156,8 @@ impl BlockwiseData {
156156

157157
pub fn send_blockwise_req(idx: Option<usize>, addr_uri: Option<(&str, &str)>, hdr: Option<&[u8]>) -> Result<BlockwiseStream, BlockwiseError> {
158158
if let Some(idx) = idx {
159-
let bs = Self::state(&idx).unwrap().get_stream();
159+
let BlockwiseState { bsd, .. } = BlockwiseState::get(idx);
160+
let mut bs = BlockwiseStream::get(idx, bsd);
160161

161162
if let Some((addr, uri)) = addr_uri { // <blockwise NEXT>
162163
let hdr = heapless::Vec::from_slice(hdr.unwrap()).unwrap();
@@ -176,15 +177,17 @@ impl BlockwiseData {
176177
// <blockwise NEW>
177178

178179
if let Some((idx, slot)) = Self::find_state_available() {
179-
let state = BlockwiseState::get(idx);
180+
*slot = Some(BlockwiseState::get(idx));
180181

181-
*slot = Some(state.clone());
182182
crate::println!("debug <blockwise NEW>, via idx={}/{}", idx, BLOCKWISE_STATES_MAX);
183183
//blockwise_states_print(); // debug
184184

185185
let (addr, uri) = addr_uri.unwrap();
186186
let req = ReqInner::new(COAP_METHOD_GET, addr, uri, None, true, Some(idx), None);
187-
let bs = state.get_stream();
187+
188+
let BlockwiseState { bsd, .. } = BlockwiseState::get(idx);
189+
let mut bs = BlockwiseStream::get(idx, bsd);
190+
bs.empty(); // a `None` item could be left over afer .close()
188191
bs.add(Some(req));
189192

190193
Ok(bs)
@@ -198,43 +201,25 @@ impl BlockwiseData {
198201

199202
#[derive(Debug)]
200203
struct BlockwiseState {
201-
idx: usize,
202-
bsd: &'static BlockwiseStreamData,
204+
bsd: &'static mut BlockwiseStreamData,
203205
addr: &'static mut [u8],
204206
uri: &'static mut [u8],
205207
hdr: &'static mut [u8],
206208
hdr_len: usize,
207209
}
208210

209-
impl Clone for BlockwiseState {
210-
fn clone(&self) -> BlockwiseState {
211-
Self {
212-
idx: self.idx,
213-
bsd: self.bsd,
214-
addr: unsafe { &mut GRID_ADDR[self.idx] },
215-
uri: unsafe { &mut GRID_URI[self.idx] },
216-
hdr: unsafe { &mut GRID_HDR[self.idx].1 },
217-
hdr_len: unsafe { GRID_HDR[self.idx].0 },
218-
}
219-
}
220-
}
221-
222211
impl BlockwiseState {
223212
fn get(idx: usize) -> Self {
224-
let bsd = unsafe { &BLOCKWISE_SD[idx] };
213+
let bsd = static_borrow_mut!(BLOCKWISE_SD[idx]);
225214

226-
Self { bsd, idx,
215+
Self { bsd,
227216
addr: unsafe { &mut GRID_ADDR[idx] },
228217
uri: unsafe { &mut GRID_URI[idx] },
229218
hdr: unsafe { &mut GRID_HDR[idx].1 },
230219
hdr_len: unsafe { GRID_HDR[idx].0 },
231220
}
232221
}
233222

234-
fn get_stream(&self) -> BlockwiseStream {
235-
BlockwiseStream::get(self.idx, self.bsd)
236-
}
237-
238223
fn update_metadata(data_in: &[u8], data: &mut [u8], data_max: usize) -> usize {
239224
let data_len = data_in.len();
240225
assert!(data_len < data_max);
@@ -251,27 +236,26 @@ impl BlockwiseState {
251236
#[derive(Debug)]
252237
pub struct BlockwiseStream {
253238
idx: usize,
254-
xs: XbdStream<Option<ReqInner>>,
239+
xs: XStream<Option<ReqInner>, 2>,
255240
}
256241

257242
impl BlockwiseStream {
258-
fn get(idx: usize, bsd: &'static BlockwiseStreamData) -> Self {
259-
let xs = XbdStream::get(bsd)
260-
.unwrap_or_else(|| XbdStream::new_with_cap(&bsd, 1));
243+
fn get(idx: usize, mut bsd: &'static mut BlockwiseStreamData) -> Self {
244+
let xs = XStream::get(static_borrow_mut!(bsd));
261245

262246
Self { idx, xs }
263247
}
264248

265-
fn add(&self, req: Option<ReqInner>) {
249+
fn add(&mut self, req: Option<ReqInner>) {
266250
assert_eq!(self.xs.len(), 0);
267251
self.xs.add(req);
268252
}
269253

270-
fn empty(&self) {
254+
fn empty(&mut self) {
271255
self.xs.empty();
272256
}
273257

274-
pub fn close(&self) {
258+
pub fn close(&mut self) {
275259
BlockwiseData::clear_state(self.idx);
276260
BlockwiseData::invalidate_state(self.idx);
277261

0 commit comments

Comments
 (0)