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
//! Reader part of the esp8266 WiFi implementation.

use core::{
    fmt::{self, Write},
    ops::Deref,
};

use embedded_hal::serial;
use heapless::Vec;

use crate::Error;

#[derive(Debug)]
pub(crate) struct ReaderPart<Rx, const N: usize> {
    rx: Rx,
    buf: Vec<u8, N>,
}

impl<Rx, const N: usize> ReaderPart<Rx, N> {
    pub fn buf(&self) -> &Vec<u8, N> {
        &self.buf
    }

    pub fn buf_mut(&mut self) -> &mut Vec<u8, N> {
        &mut self.buf
    }
}

impl<Rx, const N: usize> ReaderPart<Rx, N>
where
    Rx: serial::Read<u8> + 'static,
{
    pub fn new(rx: Rx) -> Self {
        Self {
            rx,
            buf: Vec::new(),
        }
    }

    pub fn read_byte(&mut self) -> nb::Result<u8, crate::Error> {
        self.rx.read().map_err(|x| x.map(|_| Error::ReadBuffer))
    }

    pub fn read_bytes(&mut self) -> nb::Result<(), crate::Error> {
        loop {
            if self.buf.is_full() {
                return Err(nb::Error::WouldBlock);
            }

            let byte = self.read_byte()?;
            // Safety: we have already checked if this buffer is full,
            // a couple of lines above.
            unsafe {
                self.buf.push_unchecked(byte);
            }
        }
    }
}

/// Buffer with the incoming data received from the module over the serial port.
///
/// A user should handle this data, otherwise, it will be discarded.
pub struct ReadData<'a, const N: usize> {
    inner: &'a mut Vec<u8, N>,
    from: usize,
    to: usize,
}

struct PrintAscii<'a>(&'a [u8]);

impl<'a> fmt::Debug for PrintAscii<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_char('"')?;
        for byte in self.0 {
            f.write_char(*byte as char)?;
        }
        f.write_char('"')?;

        Ok(())
    }
}

impl<'a, const N: usize> fmt::Debug for ReadData<'a, N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ReadData")
            .field("from", &self.from)
            .field("to", &self.to)
            .field("data", &PrintAscii(self.as_ref()))
            .finish()
    }
}

impl<'a, const N: usize> ReadData<'a, N> {
    pub(crate) fn new(inner: &'a mut Vec<u8, N>) -> Self {
        let to = inner.len();
        Self { inner, from: 0, to }
    }

    pub(crate) fn subslice(&mut self, from: usize, to: usize) {
        self.from = from;
        self.to = to;
    }
}

impl<'a, const N: usize> AsRef<[u8]> for ReadData<'a, N> {
    fn as_ref(&self) -> &[u8] {
        &self.inner[self.from..self.to]
    }
}

impl<'a, const N: usize> Drop for ReadData<'a, N> {
    fn drop(&mut self) {
        self.inner.clear()
    }
}

impl<'a, const N: usize> Deref for ReadData<'a, N> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.inner.as_ref()
    }
}