esp8266_wifi_serial/
reader_part.rs

1//! Reader part of the esp8266 WiFi implementation.
2
3use core::{
4    fmt::{self, Write},
5    ops::Deref,
6};
7
8use embedded_hal::serial;
9use heapless::Vec;
10
11use crate::Error;
12
13#[derive(Debug)]
14pub(crate) struct ReaderPart<Rx, const N: usize> {
15    rx: Rx,
16    buf: Vec<u8, N>,
17}
18
19impl<Rx, const N: usize> ReaderPart<Rx, N> {
20    pub fn buf(&self) -> &Vec<u8, N> {
21        &self.buf
22    }
23
24    pub fn buf_mut(&mut self) -> &mut Vec<u8, N> {
25        &mut self.buf
26    }
27}
28
29impl<Rx, const N: usize> ReaderPart<Rx, N>
30where
31    Rx: serial::Read<u8> + 'static,
32{
33    pub fn new(rx: Rx) -> Self {
34        Self {
35            rx,
36            buf: Vec::new(),
37        }
38    }
39
40    pub fn read_byte(&mut self) -> nb::Result<u8, crate::Error> {
41        self.rx.read().map_err(|x| x.map(|_| Error::ReadBuffer))
42    }
43
44    pub fn read_bytes(&mut self) -> nb::Result<(), crate::Error> {
45        loop {
46            if self.buf.is_full() {
47                return Err(nb::Error::WouldBlock);
48            }
49
50            let byte = self.read_byte()?;
51            // Safety: we have already checked if this buffer is full,
52            // a couple of lines above.
53            unsafe {
54                self.buf.push_unchecked(byte);
55            }
56        }
57    }
58}
59
60/// Buffer with the incoming data received from the module over the serial port.
61///
62/// A user should handle this data, otherwise, it will be discarded.
63pub struct ReadData<'a, const N: usize> {
64    inner: &'a mut Vec<u8, N>,
65    from: usize,
66    to: usize,
67}
68
69struct PrintAscii<'a>(&'a [u8]);
70
71impl<'a> fmt::Debug for PrintAscii<'a> {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        f.write_char('"')?;
74        for byte in self.0 {
75            f.write_char(*byte as char)?;
76        }
77        f.write_char('"')?;
78
79        Ok(())
80    }
81}
82
83impl<'a, const N: usize> fmt::Debug for ReadData<'a, N> {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        f.debug_struct("ReadData")
86            .field("from", &self.from)
87            .field("to", &self.to)
88            .field("data", &PrintAscii(self.as_ref()))
89            .finish()
90    }
91}
92
93impl<'a, const N: usize> ReadData<'a, N> {
94    pub(crate) fn new(inner: &'a mut Vec<u8, N>) -> Self {
95        let to = inner.len();
96        Self { inner, from: 0, to }
97    }
98
99    pub(crate) fn subslice(&mut self, from: usize, to: usize) {
100        self.from = from;
101        self.to = to;
102    }
103}
104
105impl<'a, const N: usize> AsRef<[u8]> for ReadData<'a, N> {
106    fn as_ref(&self) -> &[u8] {
107        &self.inner[self.from..self.to]
108    }
109}
110
111impl<'a, const N: usize> Drop for ReadData<'a, N> {
112    fn drop(&mut self) {
113        self.inner.clear()
114    }
115}
116
117impl<'a, const N: usize> Deref for ReadData<'a, N> {
118    type Target = [u8];
119
120    fn deref(&self) -> &Self::Target {
121        self.inner.as_ref()
122    }
123}