@@ -2,7 +2,7 @@ use mcu_if::c_types::c_void;
2
2
use mcu_if:: utils:: { u8_slice_from, u8_slice_mut_from} ;
3
3
use core:: { str:: from_utf8, pin:: Pin , task:: { Context , Poll } } ;
4
4
use futures_util:: stream:: Stream ;
5
- use super :: stream:: { XbdStream , StreamData , stream_uninit } ;
5
+ use super :: stream:: { XStream , XStreamData } ;
6
6
use super :: gcoap:: { ReqInner , COAP_METHOD_GET , REQ_ADDR_MAX , REQ_URI_MAX } ;
7
7
8
8
#[ no_mangle]
@@ -76,8 +76,8 @@ static mut GRID_URI: &'static mut GridUri = &mut [[0; REQ_URI_MAX]; BLOCKWISE_ST
76
76
type GridHdr = [ ( usize , [ u8 ; BLOCKWISE_HDR_MAX ] ) ; BLOCKWISE_STATES_MAX ] ;
77
77
static mut GRID_HDR : & ' static mut GridHdr = & mut [ ( 0 , [ 0 ; BLOCKWISE_HDR_MAX ] ) ; BLOCKWISE_STATES_MAX ] ;
78
78
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 ( ) ;
81
81
static mut BLOCKWISE_SD : & ' static mut [ BlockwiseStreamData ; BLOCKWISE_STATES_MAX ] =
82
82
& mut [ ARRAY_REPEAT_VALUE_SD ; BLOCKWISE_STATES_MAX ] ;
83
83
@@ -156,7 +156,8 @@ impl BlockwiseData {
156
156
157
157
pub fn send_blockwise_req ( idx : Option < usize > , addr_uri : Option < ( & str , & str ) > , hdr : Option < & [ u8 ] > ) -> Result < BlockwiseStream , BlockwiseError > {
158
158
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) ;
160
161
161
162
if let Some ( ( addr, uri) ) = addr_uri { // <blockwise NEXT>
162
163
let hdr = heapless:: Vec :: from_slice ( hdr. unwrap ( ) ) . unwrap ( ) ;
@@ -176,15 +177,17 @@ impl BlockwiseData {
176
177
// <blockwise NEW>
177
178
178
179
if let Some ( ( idx, slot) ) = Self :: find_state_available ( ) {
179
- let state = BlockwiseState :: get ( idx) ;
180
+ * slot = Some ( BlockwiseState :: get ( idx) ) ;
180
181
181
- * slot = Some ( state. clone ( ) ) ;
182
182
crate :: println!( "debug <blockwise NEW>, via idx={}/{}" , idx, BLOCKWISE_STATES_MAX ) ;
183
183
//blockwise_states_print(); // debug
184
184
185
185
let ( addr, uri) = addr_uri. unwrap ( ) ;
186
186
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()
188
191
bs. add ( Some ( req) ) ;
189
192
190
193
Ok ( bs)
@@ -198,43 +201,25 @@ impl BlockwiseData {
198
201
199
202
#[ derive( Debug ) ]
200
203
struct BlockwiseState {
201
- idx : usize ,
202
- bsd : & ' static BlockwiseStreamData ,
204
+ bsd : & ' static mut BlockwiseStreamData ,
203
205
addr : & ' static mut [ u8 ] ,
204
206
uri : & ' static mut [ u8 ] ,
205
207
hdr : & ' static mut [ u8 ] ,
206
208
hdr_len : usize ,
207
209
}
208
210
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
-
222
211
impl BlockwiseState {
223
212
fn get ( idx : usize ) -> Self {
224
- let bsd = unsafe { & BLOCKWISE_SD [ idx] } ;
213
+ let bsd = static_borrow_mut ! ( BLOCKWISE_SD [ idx] ) ;
225
214
226
- Self { bsd, idx ,
215
+ Self { bsd,
227
216
addr : unsafe { & mut GRID_ADDR [ idx] } ,
228
217
uri : unsafe { & mut GRID_URI [ idx] } ,
229
218
hdr : unsafe { & mut GRID_HDR [ idx] . 1 } ,
230
219
hdr_len : unsafe { GRID_HDR [ idx] . 0 } ,
231
220
}
232
221
}
233
222
234
- fn get_stream ( & self ) -> BlockwiseStream {
235
- BlockwiseStream :: get ( self . idx , self . bsd )
236
- }
237
-
238
223
fn update_metadata ( data_in : & [ u8 ] , data : & mut [ u8 ] , data_max : usize ) -> usize {
239
224
let data_len = data_in. len ( ) ;
240
225
assert ! ( data_len < data_max) ;
@@ -251,27 +236,26 @@ impl BlockwiseState {
251
236
#[ derive( Debug ) ]
252
237
pub struct BlockwiseStream {
253
238
idx : usize ,
254
- xs : XbdStream < Option < ReqInner > > ,
239
+ xs : XStream < Option < ReqInner > , 2 > ,
255
240
}
256
241
257
242
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) ) ;
261
245
262
246
Self { idx, xs }
263
247
}
264
248
265
- fn add ( & self , req : Option < ReqInner > ) {
249
+ fn add ( & mut self , req : Option < ReqInner > ) {
266
250
assert_eq ! ( self . xs. len( ) , 0 ) ;
267
251
self . xs . add ( req) ;
268
252
}
269
253
270
- fn empty ( & self ) {
254
+ fn empty ( & mut self ) {
271
255
self . xs . empty ( ) ;
272
256
}
273
257
274
- pub fn close ( & self ) {
258
+ pub fn close ( & mut self ) {
275
259
BlockwiseData :: clear_state ( self . idx ) ;
276
260
BlockwiseData :: invalidate_state ( self . idx ) ;
277
261
0 commit comments