phm 0.0.2

Pretty HAL Machine
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
use phm_icd::{ToMcu, ToMcuI2c, ToMcuSpi, ToMcuUart, ToPc, ToPcI2c, ToPcSpi, ToPcUart};
use postcard::{to_stdvec_cobs, CobsAccumulator, FeedResult};
use serialport::SerialPort;
use std::{
    collections::VecDeque,
    fmt::Display,
    io::{self, ErrorKind},
    time::{Duration, Instant},
};

/// The Pretty HAL Machine
///
/// This wraps a serial port connection to an embedded machine,
/// and implements various [embedded-hal](embedded-hal) traits.
pub struct Machine {
    port: Box<dyn SerialPort>,
    cobs_buf: CobsAccumulator<512>,
    command_timeout: Duration,
    uart_rx_buf: VecDeque<u8>,
}

/// The main Error type
#[derive(Debug)]
pub enum Error {
    PhmSerial(io::Error),
    Postcard(postcard::Error),
    Timeout(Duration),

    // TODO: This probably needs some more context/nuance...
    ResponseError,
    InvalidParameter,
    Unknown,
}

impl From<postcard::Error> for Error {
    fn from(err: postcard::Error) -> Self {
        Error::Postcard(err)
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Error::PhmSerial(err)
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::PhmSerial(e) => {
                write!(f, "PhmSerialError: {}", e)
            }
            Error::Postcard(e) => {
                write!(f, "PostcardError: {}", e)
            }
            Error::Timeout(d) => {
                write!(f, "Timeout({:?})", d)
            }
            Error::ResponseError => {
                write!(f, "ResponseError")
            }
            Error::InvalidParameter => {
                write!(f, "InvalidParameterError")
            }
            Error::Unknown => {
                write!(f, "UnknownError")
            }
        }
    }
}

impl std::error::Error for Error {}

impl Machine {
    pub fn from_port(port: Box<dyn SerialPort>) -> Result<Self, Error> {
        // TODO: some kind of sanity checking? Check version, protocol,
        // signs of life, anything?
        Ok(Self {
            port,
            cobs_buf: CobsAccumulator::new(),
            command_timeout: Duration::from_secs(3),
            uart_rx_buf: Default::default(),
        })
    }

    /// Set the timeout for a full command to complete.
    ///
    /// This is not a single message timeout, but rather the timeout
    /// for a whole command (e.g. an I2C write) to execute. This is currently
    /// only checked/set host side, so endless loops on the embedded side are
    /// still possible.
    pub fn set_command_timeout(&mut self, timeout: Duration) {
        self.command_timeout = timeout;
    }

    fn poll(&mut self) -> Result<Vec<ToPc>, Error> {
        let mut responses = vec![];
        let mut buf = [0u8; 1024];

        // read from stdin and push it to the decoder
        match self.port.read(&mut buf) {
            Ok(n) if n > 0 => {
                let buf = &buf[..n];
                let mut window = &buf[..];

                'cobs: while !window.is_empty() {
                    window = match self.cobs_buf.feed::<Result<phm_icd::ToPc, ()>>(&window) {
                        FeedResult::Consumed => break 'cobs,
                        FeedResult::OverFull(new_wind) => new_wind,
                        FeedResult::DeserError(new_wind) => new_wind,
                        FeedResult::Success { data, remaining } => {
                            // Do something with `data: MyData` here.
                            if let Ok(data) = data {
                                responses.push(data);
                            } else {
                                return Err(Error::ResponseError);
                            }

                            remaining
                        }
                    };
                }
            }
            Ok(_) => {}
            Err(e) if e.kind() == ErrorKind::TimedOut => {}
            Err(e) => {
                return Err(Error::PhmSerial(e));
            }
        }
        Ok(responses)
    }
}

impl embedded_hal::blocking::i2c::Write for Machine {
    type Error = Error;

    fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Error> {
        let msg = ToMcu::I2c(ToMcuI2c::Write {
            addr: address,
            output: bytes.iter().cloned().collect(),
        });
        let ser_msg = to_stdvec_cobs(&msg)?;
        self.port.write_all(&ser_msg)?;

        let start = Instant::now();

        while start.elapsed() < self.command_timeout {
            for msg in self.poll()? {
                if let ToPc::I2c(ToPcI2c::WriteComplete { addr }) = msg {
                    if address != addr {
                        continue;
                    }
                    return Ok(());
                }
            }

            // TODO: We should probably just use the `timeout` value of the serial
            // port, (e.g. don't delay at all), but I guess this is fine for now.
            std::thread::sleep(Duration::from_millis(10));
        }

        Err(Error::Timeout(self.command_timeout))
    }
}

impl embedded_hal::blocking::i2c::Read for Machine {
    type Error = Error;

    fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
        let msg = ToMcu::I2c(ToMcuI2c::Read {
            addr: address,
            to_read: len_to_u32(buffer.len())?,
        });
        let ser_msg = to_stdvec_cobs(&msg)?;
        self.port.write_all(&ser_msg)?;

        let start = Instant::now();

        while start.elapsed() < self.command_timeout {
            for msg in self.poll()? {
                if let ToPc::I2c(ToPcI2c::Read { addr, data_read }) = msg {
                    if address != addr {
                        continue;
                    }

                    if data_read.len() != buffer.len() {
                        return Err(Error::ResponseError);
                    } else {
                        buffer.copy_from_slice(&data_read);
                        return Ok(());
                    }
                }
            }

            // TODO: We should probably just use the `timeout` value of the serial
            // port, (e.g. don't delay at all), but I guess this is fine for now.
            std::thread::sleep(Duration::from_millis(10));
        }

        Err(Error::Timeout(self.command_timeout))
    }
}

impl embedded_hal::blocking::i2c::WriteRead for Machine {
    type Error = Error;

    fn write_read(
        &mut self,
        address: u8,
        bytes: &[u8],
        buffer: &mut [u8],
    ) -> Result<(), Self::Error> {
        let msg = ToMcu::I2c(ToMcuI2c::WriteThenRead {
            addr: address,
            output: bytes.iter().cloned().collect(),
            to_read: len_to_u32(buffer.len())?,
        });
        let ser_msg = to_stdvec_cobs(&msg)?;
        self.port.write_all(&ser_msg)?;

        let start = Instant::now();

        while start.elapsed() < self.command_timeout {
            for msg in self.poll()? {
                if let ToPc::I2c(ToPcI2c::WriteThenRead { addr, data_read }) = msg {
                    if address != addr {
                        continue;
                    }

                    if data_read.len() != buffer.len() {
                        return Err(Error::ResponseError);
                    } else {
                        buffer.copy_from_slice(&data_read);
                        return Ok(());
                    }
                }
            }

            // TODO: We should probably just use the `timeout` value of the serial
            // port, (e.g. don't delay at all), but I guess this is fine for now.
            std::thread::sleep(Duration::from_millis(10));
        }

        Err(Error::Timeout(self.command_timeout))
    }
}

impl embedded_hal::blocking::spi::Write<u8> for Machine {
    type Error = Error;

    fn write(&mut self, bytes: &[u8]) -> Result<(), Error> {
        let msg = ToMcu::Spi(ToMcuSpi::Write {
            output: bytes.iter().cloned().collect(),
        });
        let ser_msg = to_stdvec_cobs(&msg)?;
        self.port.write_all(&ser_msg)?;

        let start = Instant::now();

        while start.elapsed() < self.command_timeout {
            for msg in self.poll()? {
                if let ToPc::Spi(ToPcSpi::WriteComplete) = msg {
                    return Ok(());
                }
            }

            // TODO: We should probably just use the `timeout` value of the serial
            // port, (e.g. don't delay at all), but I guess this is fine for now.
            std::thread::sleep(Duration::from_millis(10));
        }

        Err(Error::Timeout(self.command_timeout))
    }
}

impl embedded_hal::blocking::spi::Transfer<u8> for Machine {
    type Error = Error;

    fn transfer<'a>(&mut self, buffer: &'a mut [u8]) -> Result<&'a [u8], Self::Error> {
        let msg = ToMcu::Spi(ToMcuSpi::Transfer {
            output: buffer.iter().cloned().collect(),
        });
        let ser_msg = to_stdvec_cobs(&msg)?;
        self.port.write_all(&ser_msg)?;

        let start = Instant::now();

        while start.elapsed() < self.command_timeout {
            for msg in self.poll()? {
                if let ToPc::Spi(ToPcSpi::Transfer { data_read }) = msg {
                    if data_read.len() != buffer.len() {
                        return Err(Error::ResponseError);
                    } else {
                        buffer.copy_from_slice(&data_read);
                        return Ok(buffer);
                    }
                }
            }

            // TODO: We should probably just use the `timeout` value of the serial
            // port, (e.g. don't delay at all), but I guess this is fine for now.
            std::thread::sleep(Duration::from_millis(10));
        }

        Err(Error::Timeout(self.command_timeout))
    }
}

impl embedded_hal::blocking::serial::Write<u8> for Machine {
    type Error = Error;

    fn bwrite_all(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
        let msg = ToMcu::Uart(ToMcuUart::Write {
            output: bytes.iter().cloned().collect(),
        });
        let ser_msg = to_stdvec_cobs(&msg)?;
        self.port.write_all(&ser_msg)?;

        let start = Instant::now();

        while start.elapsed() < self.command_timeout {
            for msg in self.poll()? {
                if let ToPc::Uart(ToPcUart::WriteComplete) = msg {
                    return Ok(());
                }
            }

            // TODO: We should probably just use the `timeout` value of the serial
            // port, (e.g. don't delay at all), but I guess this is fine for now.
            std::thread::sleep(Duration::from_millis(10));
        }

        Err(Error::Timeout(self.command_timeout))
    }

    fn bflush(&mut self) -> Result<(), Self::Error> {
        let msg = ToMcu::Uart(ToMcuUart::Flush);
        let ser_msg = to_stdvec_cobs(&msg)?;
        self.port.write_all(&ser_msg)?;

        let start = Instant::now();

        while start.elapsed() < self.command_timeout {
            for msg in self.poll()? {
                if let ToPc::Uart(ToPcUart::WriteComplete) = msg {
                    return Ok(());
                }
            }

            // TODO: We should probably just use the `timeout` value of the serial
            // port, (e.g. don't delay at all), but I guess this is fine for now.
            std::thread::sleep(Duration::from_millis(10));
        }

        Err(Error::Timeout(self.command_timeout))
    }
}

impl embedded_hal::serial::Write<u8> for Machine {
    type Error = Error;

    fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
        embedded_hal::blocking::serial::Write::<u8>::bwrite_all(self, &[byte])
            .map_err(|_| nb::Error::WouldBlock)
    }

    fn flush(&mut self) -> nb::Result<(), Self::Error> {
        embedded_hal::blocking::serial::Write::<u8>::bflush(self).map_err(|_| nb::Error::WouldBlock)
    }
}

impl embedded_hal::serial::Read<u8> for Machine {
    type Error = Error;

    fn read(&mut self) -> nb::Result<u8, Self::Error> {
        if !self.uart_rx_buf.is_empty() {
            Ok(self.uart_rx_buf.pop_front().unwrap())
        } else {
            let msg = ToMcu::Uart(ToMcuUart::Read);
            let ser_msg = to_stdvec_cobs(&msg).unwrap();
            self.port.write_all(&ser_msg).unwrap();

            let start = Instant::now();

            while start.elapsed() < self.command_timeout {
                if let Ok(vec) = self.poll() {
                    for msg in vec {
                        if let ToPc::Uart(ToPcUart::Read { data_read }) = msg {
                            self.uart_rx_buf.extend(data_read);
                            // break 'timeout;
                            if !self.uart_rx_buf.is_empty() {
                                return Ok(self.uart_rx_buf.pop_front().unwrap());
                            } else {
                                return Err(nb::Error::WouldBlock);
                            }
                        }
                    }
                }

                // TODO: We should probably just use the `timeout` value of the serial
                // port, (e.g. don't delay at all), but I guess this is fine for now.
                std::thread::sleep(Duration::from_millis(10));
            }
            Err(nb::Error::Other(Error::Timeout(self.command_timeout)))
        }
    }
}

// TODO: This is overly accepting! We have a much smaller max message size than this.
fn len_to_u32(len: usize) -> Result<u32, Error> {
    len.try_into().map_err(|_| Error::InvalidParameter)
}