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
use std::io::Write; use std::thread::sleep; use std::time::Duration; use crate::encoder::SlipEncoder; use crate::error::{Error, RomError}; use binread::io::Cursor; use binread::{BinRead, BinReaderExt}; use serial::{BaudRate, SerialPort, SerialPortSettings}; use slip_codec::Decoder; pub struct Connection { serial: Box<dyn SerialPort>, decoder: Decoder, } #[derive(Debug, Copy, Clone, BinRead)] pub struct CommandResponse { pub resp: u8, pub return_op: u8, pub return_length: u16, pub value: u32, pub status: u8, pub error: u8, } impl Connection { pub fn new(serial: impl SerialPort + 'static) -> Self { Connection { serial: Box::new(serial), decoder: Decoder::new(), } } pub fn reset(&mut self) -> Result<(), Error> { sleep(Duration::from_millis(100)); self.serial.set_dtr(false)?; self.serial.set_rts(true)?; sleep(Duration::from_millis(100)); self.serial.set_rts(false)?; Ok(()) } pub fn reset_to_flash(&mut self) -> Result<(), Error> { self.serial.set_dtr(false)?; self.serial.set_rts(true)?; sleep(Duration::from_millis(100)); self.serial.set_dtr(true)?; self.serial.set_rts(false)?; sleep(Duration::from_millis(50)); self.serial.set_dtr(true)?; Ok(()) } pub fn set_timeout(&mut self, timeout: Duration) -> Result<(), Error> { self.serial.set_timeout(timeout)?; Ok(()) } pub fn set_baud(&mut self, speed: BaudRate) -> Result<(), Error> { self.serial .reconfigure(&|setup: &mut dyn SerialPortSettings| setup.set_baud_rate(speed))?; Ok(()) } pub fn with_timeout<T, F: FnMut(&mut Connection) -> Result<T, Error>>( &mut self, timeout: Duration, mut f: F, ) -> Result<T, Error> { let old_timeout = self.serial.timeout(); self.serial.set_timeout(timeout)?; let result = f(self); self.serial.set_timeout(old_timeout)?; result } pub fn read_response(&mut self) -> Result<Option<CommandResponse>, Error> { let response = self.read()?; if response.len() < 10 { return Ok(None); } let mut cursor = Cursor::new(response); let header = cursor.read_le()?; Ok(Some(header)) } pub fn write_command( &mut self, command: u8, data: impl LazyBytes<Box<dyn SerialPort>>, check: u32, ) -> Result<(), Error> { let mut encoder = SlipEncoder::new(&mut self.serial)?; encoder.write(&[0])?; encoder.write(&[command])?; encoder.write(&(data.length().to_le_bytes()))?; encoder.write(&(check.to_le_bytes()))?; data.write(&mut encoder)?; encoder.finish()?; Ok(()) } pub fn command<Data: LazyBytes<Box<dyn SerialPort>>>( &mut self, command: u8, data: Data, check: u32, ) -> Result<u32, Error> { self.write_command(command, data, check)?; match self.read_response()? { Some(response) if response.return_op == command as u8 => { if response.status == 1 { Err(Error::RomError(RomError::from(response.error))) } else { Ok(response.value) } } _ => Err(Error::ConnectionFailed), } } fn read(&mut self) -> Result<Vec<u8>, Error> { let mut output = Vec::with_capacity(1024); self.decoder.decode(&mut self.serial, &mut output)?; Ok(output) } pub fn flush(&mut self) -> Result<(), Error> { self.serial.flush()?; Ok(()) } } pub trait LazyBytes<W: Write> { fn write(self, encoder: &mut SlipEncoder<W>) -> Result<(), Error>; fn length(&self) -> u16; } impl<W: Write> LazyBytes<W> for &[u8] { fn write(self, encoder: &mut SlipEncoder<W>) -> Result<(), Error> { encoder.write(self)?; Ok(()) } fn length(&self) -> u16 { self.len() as u16 } } impl<W: Write, F: Fn(&mut SlipEncoder<W>) -> Result<(), Error>> LazyBytes<W> for (u16, F) { fn write(self, encoder: &mut SlipEncoder<W>) -> Result<(), Error> { self.1(encoder) } fn length(&self) -> u16 { self.0 } }