futuresdr 0.0.41

An Experimental Async SDR Runtime for Heterogeneous Architectures.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use std::any::Any;
use std::fmt::Debug;
use std::ops::Deref;
use std::ops::DerefMut;

use crate::runtime::BlockId;
use crate::runtime::BlockMessage;
use crate::runtime::Error;
use crate::runtime::Pmt;
use crate::runtime::PortId;
use crate::runtime::buffer::BufferReader;
use crate::runtime::buffer::BufferWriter;
use crate::runtime::buffer::CpuBufferReader;
use crate::runtime::buffer::CpuBufferWriter;
use crate::runtime::buffer::CpuSample;
use crate::runtime::buffer::Tags;
use crate::runtime::channel::mpsc::Receiver;
use crate::runtime::channel::mpsc::channel;
use crate::runtime::config::config;
use crate::runtime::dev::BlockInbox;
use crate::runtime::dev::BlockMeta;
use crate::runtime::dev::BlockNotifier;
use crate::runtime::dev::ItemTag;
use crate::runtime::dev::MessageOutputs;
use crate::runtime::dev::WorkIo;
use crate::runtime::kernel_interface::KernelInterface;
use crate::runtime::wrapped_kernel::WrappedKernel;

/// Native test harness for running one block without a [`Runtime`](crate::runtime::Runtime).
///
/// `Mocker` wraps a kernel in the same block wrapper used by the runtime, but
/// drives `init`, `work`, message handlers, and `deinit` directly. It is useful
/// for focused unit tests and microbenchmarks where constructing a full
/// [`Flowgraph`](crate::runtime::Flowgraph) would add noise.
pub struct Mocker<K: KernelInterface> {
    /// Wrapped Block
    block: WrappedKernel<K>,
    message_sinks: Vec<Receiver<BlockMessage>>,
    messages: Vec<Vec<Pmt>>,
}

impl<K: KernelInterface + 'static> Deref for Mocker<K> {
    type Target = K;

    fn deref(&self) -> &Self::Target {
        &self.block.kernel
    }
}
impl<K: KernelInterface + 'static> DerefMut for Mocker<K> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.block.kernel
    }
}

impl<K: KernelInterface + crate::runtime::dev::Kernel + 'static> Mocker<K> {
    /// Get the block id.
    pub fn id(&self) -> BlockId {
        self.block.id
    }

    /// Get mutable access to the wrapped kernel state used by `Kernel::work`.
    pub fn parts_mut(&mut self) -> (&mut K, &mut MessageOutputs, &mut BlockMeta) {
        let WrappedKernel {
            kernel, mo, meta, ..
        } = &mut self.block;
        (kernel, mo, meta)
    }

    /// Get block metadata.
    pub fn meta(&self) -> &BlockMeta {
        &self.block.meta
    }

    /// Get mutable block metadata.
    pub fn meta_mut(&mut self) -> &mut BlockMeta {
        &mut self.block.meta
    }

    /// Create a mocker around one kernel instance.
    ///
    /// Message output ports declared by the block are connected to internal
    /// sinks so tests can inspect emitted PMTs with [`Mocker::messages`] or
    /// [`Mocker::take_messages`].
    pub fn new(kernel: K) -> Self {
        let mut block = WrappedKernel::new(kernel, BlockId(0));
        let mut messages = Vec::new();
        let mut message_sinks: Vec<Receiver<BlockMessage>> = Vec::new();
        let msg_len = config().queue_size;

        for n in <K as KernelInterface>::message_outputs() {
            messages.push(Vec::new());
            let (tx, rx) = channel(msg_len);
            message_sinks.push(rx);
            block
                .mo
                .connect(
                    &PortId::new(*n),
                    BlockInbox::new(tx, BlockNotifier::new()),
                    &PortId::new("input"),
                )
                .unwrap();
        }

        Mocker {
            block,
            message_sinks,
            messages,
        }
    }

    /// Call one message handler of the block and return its PMT result.
    ///
    /// This executes the generated handler dispatch directly. It does not run
    /// `work()` afterward; call [`Mocker::run`] if the handler only queued state
    /// that should be processed by the work loop.
    pub fn post(&mut self, id: impl Into<PortId>, p: Pmt) -> Result<Pmt, Error> {
        let id = id.into();
        let mut io = WorkIo {
            call_again: false,
            finished: false,
            block_on: false,
        };

        let WrappedKernel {
            meta, mo, kernel, ..
        } = &mut self.block;
        crate::runtime::block_on(kernel.call_handler(&mut io, mo, meta, id, p))
            .map_err(|e| Error::HandlerError(e.to_string()))
    }

    /// Run the block's `work()` loop synchronously.
    ///
    /// The loop repeats while the block sets [`WorkIo::call_again`]. Message
    /// outputs produced during each call are captured before the next iteration.
    pub fn run(&mut self) {
        crate::runtime::block_on(self.run_async());
    }

    /// Run the block's `init()` method synchronously.
    pub fn init(&mut self) {
        crate::runtime::block_on(async {
            self.block
                .kernel
                .init(&mut self.block.mo, &mut self.block.meta)
                .await
                .unwrap();
        });
    }

    /// Run the block's `deinit()` method synchronously.
    pub fn deinit(&mut self) {
        crate::runtime::block_on(async {
            self.block
                .kernel
                .deinit(&mut self.block.mo, &mut self.block.meta)
                .await
                .unwrap();
        });
    }

    /// Get produced PMTs from output message ports.
    pub fn messages(&self) -> Vec<Vec<Pmt>> {
        self.messages.clone()
    }

    /// Take produced PMTs from output message ports.
    pub fn take_messages(&mut self) -> Vec<Vec<Pmt>> {
        std::mem::take(&mut self.messages)
    }

    /// Run the block's `work()` loop asynchronously.
    ///
    /// Like [`Mocker::run`], this repeats while [`WorkIo::call_again`] is set.
    pub async fn run_async(&mut self) {
        let mut io = WorkIo {
            call_again: false,
            finished: false,
            block_on: false,
        };

        loop {
            self.block
                .kernel
                .work(&mut io, &mut self.block.mo, &mut self.block.meta)
                .await
                .unwrap();

            for (n, r) in self.message_sinks.iter_mut().enumerate() {
                while let Ok(m) = r.try_recv() {
                    match m {
                        BlockMessage::Call { data, .. } => {
                            self.messages[n].push(data);
                        }
                        _ => panic!("Mocked Block produced unexpected BlockMessage {m:?}"),
                    }
                }
            }

            if !io.call_again {
                break;
            } else {
                io.call_again = false;
            }
        }
    }
}

#[derive(Debug)]
/// Mock CPU input buffer for [`Mocker`].
///
/// Use [`Reader::set`] or [`Reader::set_with_tags`] before running the block.
/// Consumed items are removed from the front of the buffer, matching the normal
/// [`CpuBufferReader`] contract.
pub struct Reader<T: Debug + Send + 'static> {
    data: Vec<T>,
    tags: Vec<ItemTag>,
    block_id: BlockId,
    port_id: PortId,
}

impl<T: Debug + Send + 'static> Reader<T> {
    /// Replace the readable input items.
    pub fn set(&mut self, data: Vec<T>)
    where
        T: Debug + Send + 'static,
    {
        self.set_with_tags(data, Vec::new());
    }

    /// Replace the readable input items and their tags.
    pub fn set_with_tags(&mut self, data: Vec<T>, tags: Vec<ItemTag>)
    where
        T: Debug + Send + 'static,
    {
        self.data = data;
        self.tags = tags;
    }
}

impl<T: Debug + Send + 'static> Default for Reader<T> {
    fn default() -> Self {
        Self {
            data: vec![],
            tags: vec![],
            block_id: BlockId(0),
            port_id: PortId::new("input"),
        }
    }
}

impl<T: Debug + Send + 'static> BufferReader for Reader<T> {
    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
    fn init(&mut self, block_id: BlockId, port_id: PortId, _inbox: BlockInbox) {
        self.block_id = block_id;
        self.port_id = port_id;
    }
    fn validate(&self) -> Result<(), Error> {
        Ok(())
    }
    async fn notify_finished(&mut self) {}
    fn finish(&mut self) {}
    fn finished(&self) -> bool {
        true
    }
    fn block_id(&self) -> BlockId {
        self.block_id
    }
    fn port_id(&self) -> PortId {
        self.port_id.clone()
    }
}

impl<T> CpuBufferReader for Reader<T>
where
    T: CpuSample,
{
    type Item = T;

    fn slice(&mut self) -> &[Self::Item] {
        self.data.as_slice()
    }
    fn slice_with_tags(&mut self) -> (&[Self::Item], &Vec<ItemTag>) {
        (self.data.as_slice(), &self.tags)
    }
    fn consume(&mut self, n: usize) {
        self.data = self.data.split_off(n);
        self.tags.retain(|x| x.index >= n);

        for t in self.tags.iter_mut() {
            t.index -= n;
        }
    }

    fn set_min_items(&mut self, _n: usize) {
        warn!("set_min_items has no effect in with mocker");
    }

    fn set_min_buffer_size_in_items(&mut self, _n: usize) {
        warn!("set_min_buffer_size_in_items has no effect in a mocker");
    }

    fn max_items(&self) -> usize {
        self.data.len()
    }
}

#[derive(Debug)]
/// Mock CPU output buffer for [`Mocker`].
///
/// Reserve capacity before running the block. Produced items are appended to the
/// buffer and can be cloned with [`Writer::get`] or drained with
/// [`Writer::take`].
pub struct Writer<T: Clone + Debug + Send + 'static> {
    data: Vec<T>,
    tags: Vec<ItemTag>,
    block_id: BlockId,
    port_id: PortId,
}

impl<T: Clone + Debug + Send + 'static> Default for Writer<T> {
    fn default() -> Self {
        Self {
            data: vec![],
            tags: vec![],
            block_id: BlockId(0),
            port_id: PortId::new("output"),
        }
    }
}

impl<T: Clone + Debug + Send + 'static> Writer<T> {
    /// Reserve writable capacity in the output buffer.
    pub fn reserve(&mut self, n: usize) {
        self.data = Vec::with_capacity(n);
    }
    /// Clone all produced items and tags without clearing them.
    pub fn get(&self) -> (Vec<T>, Vec<ItemTag>) {
        (self.data.clone(), self.tags.clone())
    }
    /// Drain all produced items and tags.
    pub fn take(&mut self) -> (Vec<T>, Vec<ItemTag>) {
        (
            std::mem::take(&mut self.data),
            std::mem::take(&mut self.tags),
        )
    }
}

impl<T: Clone + Debug + Send + 'static> BufferWriter for Writer<T> {
    type Reader = Reader<T>;

    fn init(&mut self, block_id: BlockId, port_id: PortId, _inbox: BlockInbox) {
        self.block_id = block_id;
        self.port_id = port_id;
    }
    fn validate(&self) -> Result<(), Error> {
        Ok(())
    }
    fn connect(&mut self, _dest: &mut Self::Reader) {}

    async fn notify_finished(&mut self) {}

    fn block_id(&self) -> BlockId {
        self.block_id
    }

    fn port_id(&self) -> PortId {
        self.port_id.clone()
    }
}

impl<T> CpuBufferWriter for Writer<T>
where
    T: CpuSample,
{
    type Item = T;

    fn slice(&mut self) -> &mut [Self::Item] {
        unsafe {
            std::slice::from_raw_parts_mut(
                self.data.as_mut_ptr().add(self.data.len()),
                self.data.capacity() - self.data.len(),
            )
        }
    }
    fn slice_with_tags(&mut self) -> (&mut [Self::Item], Tags<'_>) {
        let s = unsafe {
            std::slice::from_raw_parts_mut(
                self.data.as_mut_ptr().add(self.data.len()),
                self.data.capacity() - self.data.len(),
            )
        };
        (s, Tags::new(&mut self.tags, self.data.len()))
    }
    fn produce(&mut self, n: usize) {
        let curr_len = self.data.len();
        unsafe {
            self.data.set_len(curr_len + n);
        }
    }

    fn set_min_items(&mut self, _n: usize) {
        warn!("set_min_items has no effect in with mocker");
    }

    fn set_min_buffer_size_in_items(&mut self, _n: usize) {
        warn!("set_min_buffer_size_in_items has no effect in a mocker");
    }

    fn max_items(&self) -> usize {
        self.data.len()
    }
}