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
pub extern crate lin_bus;
pub extern crate serial;

use lin_bus::{driver, PID};
use serial::{SerialPort, SystemPort};
use std::io::{Read, Write};
use std::thread::sleep;
use std::time::Duration;

pub struct SerialLin(pub SystemPort);

#[derive(Debug)]
pub enum SerialError {
    LinError(driver::Error),
    SerialError(serial::Error),
}

impl From<serial::Error> for SerialError {
    fn from(error: serial::Error) -> SerialError {
        SerialError::SerialError(error)
    }
}

impl From<SerialError> for driver::Error {
    fn from(error: SerialError) -> driver::Error {
        match error {
            SerialError::LinError(lin_error) => lin_error,
            SerialError::SerialError(ser_error) => match ser_error.kind() {
                serial::ErrorKind::Io(std::io::ErrorKind::TimedOut) => driver::Error::Timeout,
                _ => driver::Error::PhysicalBus,
            },
        }
    }
}

impl From<driver::Error> for SerialError {
    fn from(error: driver::Error) -> SerialError {
        SerialError::LinError(error)
    }
}

impl From<std::io::Error> for SerialError {
    fn from(error: std::io::Error) -> SerialError {
        SerialError::SerialError(serial::Error::from(error))
    }
}

impl driver::Master for SerialLin {
    type Error = SerialError;

    fn send_wakeup(&mut self) -> Result<(), SerialError> {
        self.0.set_timeout(Duration::from_millis(1000))?;

        self.0.reconfigure(&|settings| {
            settings.set_baud_rate(serial::Baud9600)?;
            settings.set_char_size(serial::Bits7);
            settings.set_parity(serial::ParityNone);
            settings.set_stop_bits(serial::Stop1);
            settings.set_flow_control(serial::FlowNone);
            Ok(())
        })?;

        self.0.write(&[0])?;
        let mut buf = [0; 1];
        self.0.read_exact(&mut buf)?;

        if buf[0] != 0 {
            Err(SerialError::LinError(driver::Error::PhysicalBus))
        } else {
            sleep(Duration::from_millis(100));
            Ok(())
        }
    }

    fn send_header(&mut self, pid: PID) -> Result<(), SerialError> {
        self.0.set_timeout(Duration::from_millis(1000))?;

        self.0.reconfigure(&|settings| {
            settings.set_baud_rate(serial::Baud9600)?;
            settings.set_char_size(serial::Bits7);
            settings.set_parity(serial::ParityNone);
            settings.set_stop_bits(serial::Stop1);
            settings.set_flow_control(serial::FlowNone);
            Ok(())
        })?;

        self.0.write(&[0])?;
        // wait a short time before switching baudrate again, otherwise the zero byte won't be sent
        // with the lower baudrate
        sleep(Duration::from_millis(1));

        self.0.reconfigure(&|settings| {
            settings.set_baud_rate(serial::Baud19200)?;
            settings.set_char_size(serial::Bits8);
            settings.set_parity(serial::ParityNone);
            settings.set_stop_bits(serial::Stop1);
            settings.set_flow_control(serial::FlowNone);
            Ok(())
        })?;

        self.0.write(&[0x55, pid.get()])?;

        let mut buf = [0; 2];
        self.0.read_exact(&mut buf)?;

        if buf != [0x55, pid.get()] {
            Err(SerialError::LinError(driver::Error::PhysicalBus))
        } else {
            Ok(())
        }
    }

    fn read(&mut self, buf: &mut [u8]) -> Result<(), SerialError> {
        self.0.read_exact(buf)?;
        Ok(())
    }
    fn write(&mut self, data: &[u8]) -> Result<(), SerialError> {
        assert!(
            data.len() <= 9,
            "Data must be at most 8 bytes + 1 checksum byte"
        );
        self.0.write(data)?;
        let mut buf = [0; 9];
        self.0.read_exact(&mut buf[0..data.len()])?;
        if &buf[0..data.len()] != data {
            Err(SerialError::LinError(driver::Error::PhysicalBus))
        } else {
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}