capsules_core/test/
virtual_uart.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Test reception on the virtualized UART: best if multiple Tests are
6//! instantiated and tested in parallel.
7use crate::virtualizers::virtual_uart::UartDevice;
8
9use kernel::debug;
10use kernel::hil::uart;
11use kernel::hil::uart::Receive;
12use kernel::utilities::cells::TakeCell;
13use kernel::ErrorCode;
14
15pub struct TestVirtualUartReceive {
16    device: &'static UartDevice<'static>,
17    buffer: TakeCell<'static, [u8]>,
18}
19
20impl TestVirtualUartReceive {
21    pub fn new(device: &'static UartDevice<'static>, buffer: &'static mut [u8]) -> Self {
22        TestVirtualUartReceive {
23            device,
24            buffer: TakeCell::new(buffer),
25        }
26    }
27
28    pub fn run(&self) {
29        let buf = self.buffer.take().unwrap();
30        let len = buf.len();
31        debug!("Starting receive of length {}", len);
32        self.device
33            .receive_buffer(buf, len)
34            .expect("Calling receive_buffer() in virtual_uart test failed");
35    }
36}
37
38impl uart::ReceiveClient for TestVirtualUartReceive {
39    fn received_buffer(
40        &self,
41        rx_buffer: &'static mut [u8],
42        rx_len: usize,
43        rcode: Result<(), ErrorCode>,
44        _error: uart::Error,
45    ) {
46        debug!("Virtual uart read complete: {:?}: ", rcode);
47        for i in 0..rx_len {
48            debug!("{:02x} ", rx_buffer[i]);
49        }
50        debug!("Starting receive of length {}", rx_len);
51        self.device
52            .receive_buffer(rx_buffer, rx_len)
53            .expect("Calling receive_buffer() in virtual_uart test failed");
54    }
55}