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
use heapless::{consts, spsc::Consumer, String};

use embedded_hal::{serial, timer::CountDown};

use crate::error::{Error, NBResult, Result};
use crate::traits::{ATATCmd, ATATInterface};
use crate::Mode;

type ResConsumer = Consumer<'static, Result<String<consts::U256>>, consts::U10, u8>;

#[derive(Debug)]
enum ClientState {
    Idle,
    AwaitingResponse,
}

pub struct ATClient<Tx, T>
where
    Tx: serial::Write<u8>,
    T: CountDown,
{
    tx: Tx,
    res_c: ResConsumer,
    // last_response_time: T::Time,
    state: ClientState,
    mode: Mode<T>,
}

impl<Tx, T> ATClient<Tx, T>
where
    Tx: serial::Write<u8>,
    T: CountDown,
{
    pub fn new(tx: Tx, queue: ResConsumer, mode: Mode<T>) -> Self {
        Self {
            tx,
            res_c: queue,
            state: ClientState::Idle,
            mode,
        }
    }
}

impl<Tx, T> ATATInterface for ATClient<Tx, T>
where
    Tx: serial::Write<u8>,
    T: CountDown,
    T::Time: From<u32>,
{
    fn send<A: ATATCmd>(&mut self, cmd: &A) -> NBResult<A::Response> {
        if let ClientState::Idle = self.state {
            for c in cmd.as_str().as_bytes() {
                block!(self.tx.write(*c)).ok();
            }
            block!(self.tx.flush()).ok();
            self.state = ClientState::AwaitingResponse;
        }

        match self.mode {
            Mode::Blocking => Ok(block!(self.check_response(cmd)).unwrap()),
            Mode::NonBlocking => self.check_response(cmd),
            Mode::Timeout(ref mut timer) => {
                timer.start(cmd.max_timeout_ms());
                Ok(block!(self.check_response(cmd)).unwrap())
            }
        }
    }

    fn check_response<A: ATATCmd>(&mut self, cmd: &A) -> NBResult<A::Response> {
        if let Some(result) = self.res_c.dequeue() {
            match result {
                Ok(resp) => {
                    if let ClientState::AwaitingResponse = self.state {
                        self.state = ClientState::Idle;
                        cmd.parse(&resp).map_err(nb::Error::Other)
                    } else {
                        // URC
                        Err(nb::Error::WouldBlock)
                    }
                }
                Err(e) => Err(nb::Error::Other(e)),
            }
        } else if let Mode::Timeout(ref mut timer) = self.mode {
            if timer.wait().is_ok() {
                self.state = ClientState::Idle;
                Err(nb::Error::Other(Error::Timeout))
            } else {
                Err(nb::Error::WouldBlock)
            }
        } else {
            Err(nb::Error::WouldBlock)
        }
    }
}