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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
extern crate core;
#[macro_use]
extern crate log;
extern crate serialport;

use core::num;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::sync::mpsc::{channel, Sender, Receiver, TryRecvError};
use std::thread;
use std::time::{Duration, Instant};

pub use self::serialport::prelude::*;

#[derive(Debug)]
pub enum SerialCommand {
    CancelSendFile,
    ChangeBaud(u32),
    ChangeDataBits(DataBits),
    ChangeFlowControl(FlowControl),
    ChangeStopBits(StopBits),
    ChangeParity(Parity),
    ChangePort(String),
    ConnectToPort { name: String, baud: u32 },
    Disconnect,
    SendData(Vec<u8>),
    SendFile(PathBuf),
    LogToFile(PathBuf),
    CancelLogToFile,
}

#[derive(Debug)]
pub enum SerialResponse {
    Data(Vec<u8>),
    SendingFileCanceled,
    SendingFileComplete,
    /// Response to `SerialCommand::SendFile`. Confirms that the file has been opened successfully
    /// and data is going to be sent.
    SendingFileStarted,
    /// Status response indicating what percentage of transmission a file is at
    SendingFileProgress(u8),
    SendingFileError(String),
    OpenPortSuccess(String),
    OpenPortError(String),
    DisconnectSuccess,
    LogToFileError(String),
    LoggingFileCanceled,
    /// A port error has occurred that is likely the result of a serial device disconnected. This
    /// also returns a list of all still-attached serial devices.
    UnexpectedDisconnection(Vec<String>),
    /// A sorted list of ports found during a port scan. Guaranteed to contain the currently-active
    /// port if there is one.
    PortsFound(Vec<String>),
}

#[derive(Debug)]
pub enum GeneralError {
    Parse(num::ParseIntError),
    Send(SerialCommand),
}

pub enum ReadBytes {
    /// Number of bytes read from the file, 0 if none but not an error
    Bytes(usize),
    /// The end of the file has been reached so no data was read
    EndOfFile,
    /// An error was encountered reading from the file
    FileError,
    /// No read was actually attempted
    NoAttempt,
}

pub struct SerialThread {
    pub from_port_chan_rx: Receiver<SerialResponse>,
    pub to_port_chan_tx: Sender<SerialCommand>,
}

pub fn list_ports() -> serialport::Result<Vec<String>> {
    match serialport::available_ports() {
        Ok(ports) => Ok(ports.into_iter().map(|x| x.port_name).collect()),
        Err(e) => Err(e),
    }
}

impl SerialThread {
    pub fn new<F: Fn() + Send + 'static>(callback: F) -> Self {

        let (from_port_chan_tx, from_port_chan_rx) = channel();
        let (to_port_chan_tx, to_port_chan_rx) = channel();

        // Open a thread to monitor the active serial channel. This thread is always-running and
        // listening for various port-related commands, but is not necessarily always connected to
        // the port.
        thread::spawn(move || {
            let mut port: Option<Box<SerialPort>> = None;
            let mut read_file: Option<Box<File>> = None;
            let mut bytes_read = 0u64;
            let mut bytes_total = 0u64;
            let mut last_percentage = 0u8;
            let mut write_file: Option<Box<File>> = None;

            let mut serial_buf: Vec<u8> = vec![0; 1000];
            let mut serial_buf_rx = [0; 1000];
            let mut last_send_time = Instant::now();
            let mut settings: SerialPortSettings = Default::default();

            let loop_time = 10usize; // ms
            let port_scan_time = Duration::from_secs(5);
            let mut last_port_scan_time = Instant::now();

            loop {
                // First check if we have any incoming commands
                match to_port_chan_rx.try_recv() {
                    Ok(SerialCommand::ConnectToPort { name, baud }) => {
                        settings.baud_rate = BaudRate::from_speed(baud as usize);
                        info!("Connecting to {} at {} with settings {:?}",
                              &name,
                              &baud,
                              &settings);
                        match serialport::open_with_settings(&name, &settings) {
                            Ok(p) => {
                                port = Some(p);
                                from_port_chan_tx
                                    .send(SerialResponse::OpenPortSuccess(name))
                                    .unwrap();
                            }
                            Err(serialport::Error {kind: serialport::ErrorKind::NoDevice, ..}) => {
                                let err_str = String::from(format!("Port '{}' is already in use \
                                                                    or doesn't exist", &name));
                                let error = SerialResponse::OpenPortError(err_str);
                                from_port_chan_tx.send(error).unwrap();
                            }
                            Err(e) => {
                                from_port_chan_tx.send(SerialResponse::OpenPortError(e.description))
                                    .unwrap();
                            }
                        }
                        callback();
                    }
                    Ok(SerialCommand::ChangeBaud(baud)) => {
                        info!("Changing baud to {}", baud);
                        let baud_rate = BaudRate::from_speed(baud as usize);
                        settings.baud_rate = baud_rate;
                        if let Some(ref mut p) = port {
                            p.set_baud_rate(baud_rate).unwrap();
                        }
                    }
                    Ok(SerialCommand::ChangeDataBits(data_bits)) => {
                        info!("Changing data bits to {:?}", data_bits);
                        settings.data_bits = data_bits;
                        if let Some(ref mut p) = port {
                            p.set_data_bits(data_bits).unwrap();
                        }
                    }
                    Ok(SerialCommand::ChangeFlowControl(flow_control)) => {
                        info!("Changing flow control to {:?}", flow_control);
                        settings.flow_control = flow_control;
                        if let Some(ref mut p) = port {
                            p.set_flow_control(flow_control).unwrap();
                        }
                    }
                    Ok(SerialCommand::ChangeStopBits(stop_bits)) => {
                        info!("Changing stop bits to {:?}", stop_bits);
                        settings.stop_bits = stop_bits;
                        if let Some(ref mut p) = port {
                            p.set_stop_bits(stop_bits).unwrap();
                        }
                    }
                    Ok(SerialCommand::ChangeParity(parity)) => {
                        info!("Changing parity to {:?}", parity);
                        settings.parity = parity;
                        if let Some(ref mut p) = port {
                            p.set_parity(parity).unwrap();
                        }
                    }
                    Ok(SerialCommand::ChangePort(name)) => {
                        if port.is_some() {
                            info!("Changing port to '{}' using settings {:?}",
                                  &name,
                                  &settings);

                            match serialport::open_with_settings(&name, &settings) {
                                Ok(p) => {
                                    port = Some(p);
                                    from_port_chan_tx
                                        .send(SerialResponse::OpenPortSuccess(name))
                                        .unwrap();
                                }
                                Err(_) => {
                                    port = None;
                                    let err_str = String::from(format!("Failed to open port '{}'",
                                                                       &name));
                                    let error = SerialResponse::OpenPortError(err_str);
                                    from_port_chan_tx.send(error).unwrap();
                                    callback();
                                }
                            }
                        }
                    }
                    Ok(SerialCommand::Disconnect) => {
                        info!("Disconnecting");
                        port = None;
                        read_file = None;
                        write_file = None;
                        from_port_chan_tx.send(SerialResponse::DisconnectSuccess).unwrap();
                        callback();
                    }
                    Ok(SerialCommand::SendData(d)) => {
                        if let Some(ref mut p) = port {
                            match p.write(d.as_ref()) {
                                Ok(_) => (),
                                Err(e) => error!("Error in SendData: {:?}", e),
                            }
                        }
                    }
                    Ok(SerialCommand::SendFile(f)) => {
                        if port.is_some() {
                            bytes_total = fs::metadata(&f).unwrap().len();
                            last_percentage = 0;
                            bytes_read = 0;
                            info!("Sending file {:?} ({} bytes)", f, bytes_total);
                            match File::open(f) {
                                Ok(file) => read_file = Some(Box::new(file)),
                                Err(e) => error!("{:?}", e),
                            }
                            from_port_chan_tx.send(SerialResponse::SendingFileStarted).unwrap();
                            callback();
                        } else {
                            let err_str = String::from("No open port to send file");
                            let error = SerialResponse::SendingFileError(err_str);
                            from_port_chan_tx.send(error).unwrap();
                            callback();
                        }
                    }
                    Ok(SerialCommand::CancelSendFile) => {
                        read_file = None;
                        from_port_chan_tx.send(SerialResponse::SendingFileCanceled).unwrap();
                        callback();
                    }
                    Ok(SerialCommand::LogToFile(f)) => {
                        if port.is_some() {
                            info!("Logging to file {:?}", f);
                            match File::create(f) {
                                Ok(file) => write_file = Some(Box::new(file)),
                                Err(e) => error!("{:?}", e),
                            }
                        } else {
                            let err_str = String::from("No open port to log file from");
                            let error = SerialResponse::LogToFileError(err_str);
                            from_port_chan_tx.send(error).unwrap();
                            callback();
                        }
                    }
                    Ok(SerialCommand::CancelLogToFile) => {
                        write_file = None;
                        from_port_chan_tx.send(SerialResponse::LoggingFileCanceled).unwrap();
                        callback();
                    }
                    Err(TryRecvError::Empty) |
                    Err(TryRecvError::Disconnected) => (),
                }

                // If a port is active, handle reading and writing to it.
                if let Some(ref mut p) = port {
                    // Read data from the port
                    let rx_data_len = match p.read(serial_buf.as_mut_slice()) {
                        Ok(t) => t,
                        Err(_) => 0,
                    };

                    // And send this data over the channel
                    if rx_data_len > 0 {
                        let send_data = SerialResponse::Data(serial_buf[..rx_data_len].to_vec());
                        from_port_chan_tx.send(send_data).unwrap();
                        callback();

                        // Write the data to a log file if one's set up
                        if let Some(ref mut file) = write_file {
                            match file.write(&serial_buf[..rx_data_len]) {
                                Err(e) => error!("{:?}", e),
                                Ok(l) => {
                                    if l < rx_data_len {
                                        warn!("Only {}/{} bytes logged", l, rx_data_len);
                                    }
                                }
                            }
                        }
                    }

                    // If a file has been opened, read the next 1ms of data from
                    // it as determined by the current baud rate.
                    let mut read_len: ReadBytes = ReadBytes::NoAttempt;
                    let mut byte_as_serial_bits = 1 + 8;
                    if p.parity().unwrap() != Parity::None {
                        byte_as_serial_bits += 1;
                    }
                    if p.stop_bits().unwrap() == StopBits::One {
                        byte_as_serial_bits += 1;
                    } else if p.stop_bits().unwrap() == StopBits::Two {
                        byte_as_serial_bits += 2;
                    }
                    // Write 10ms of data at a time to account for loop time
                    // variation
                    if last_send_time.elapsed().subsec_nanos() > loop_time as u32 * 1_000_000 {
                        let baud: u32 = p.baud_rate().unwrap().speed() as u32;
                        let tx_data_len = baud as usize / byte_as_serial_bits / (1000 / loop_time);
                        if let Some(ref mut file) = read_file {
                            debug!("Reading {} bytes", tx_data_len);
                            match file.read(&mut serial_buf_rx[..tx_data_len]) {
                                Ok(0) => {
                                    debug!("END OF FILE!");
                                    read_len = ReadBytes::EndOfFile;
                                }
                                Ok(len) => {
                                    bytes_read += len as u64;
                                    debug!("Actually read {} bytes ({} total)", len, bytes_read);
                                    read_len = ReadBytes::Bytes(len);
                                    let percentage =
                                        (bytes_read as f32 / bytes_total as f32 * 100.0) as u8;
                                    if percentage >= last_percentage + 5 {
                                        from_port_chan_tx
                                            .send(SerialResponse::SendingFileProgress(percentage))
                                            .unwrap();
                                        callback();
                                        last_percentage = percentage;
                                    }
                                }
                                Err(e) => {
                                    error!("File error trying to read {} bytes", tx_data_len);
                                    error!("{:?}", e);
                                    read_len = ReadBytes::FileError;
                                }
                            }
                        }
                    }

                    match read_len {
                        ReadBytes::Bytes(x) => {
                            if p.write(&serial_buf_rx[..x]).is_err() {
                                // TODO: Check if port still exists and send different error if so
                                let err_str = format!("Failed to send {} bytes", x);
                                error!("Failed to send {} bytes", x);
                                read_file = None;
                                from_port_chan_tx.send(SerialResponse::SendingFileError(err_str))
                                    .unwrap();
                                callback();
                            }
                            last_send_time = Instant::now();
                        }
                        ReadBytes::EndOfFile | ReadBytes::FileError => {
                            read_file = None;
                            from_port_chan_tx.send(SerialResponse::SendingFileComplete).unwrap();
                            callback();
                        }
                        ReadBytes::NoAttempt => (),
                    }
                }

                // Scan for ports every so often
                if last_port_scan_time.elapsed() > port_scan_time {
                    last_port_scan_time = Instant::now();
                    let mut ports = list_ports().expect("Scanning for ports should never fail");
                    ports.sort();
                    debug!("Found ports: {:?}", &ports);

                    // Check if our port was disconnected
                    let message = {
                        if let Some(ref mut p) = port {
                            if let Some(name) = p.port_name() {
                                if ports.binary_search(&name).is_err() {
                                    SerialResponse::UnexpectedDisconnection(ports)
                                } else {
                                    SerialResponse::PortsFound(ports)
                                }
                            } else {
                                SerialResponse::PortsFound(ports)
                            }
                        } else {
                            SerialResponse::PortsFound(ports)
                        }
                    };
                    match message {
                        SerialResponse::UnexpectedDisconnection(_) => port = None,
                        _ => (),
                    };
                    from_port_chan_tx.send(message).unwrap();
                    callback();
                }

                thread::sleep(Duration::from_millis(loop_time as u64));
            }
        });

        SerialThread {
            from_port_chan_rx: from_port_chan_rx,
            to_port_chan_tx: to_port_chan_tx,
        }
    }

    pub fn send_port_open_cmd(&self,
                              port_name: String,
                              baud_rate: String)
                              -> Result<(), GeneralError> {
        let baud_rate: u32 = baud_rate.parse().map_err(GeneralError::Parse)?;
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::ConnectToPort {
                      name: port_name,
                      baud: baud_rate,
                  })
            .map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_port_close_cmd(&self) -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::Disconnect).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_port_change_baud_cmd(&self, baud_rate: String) -> Result<(), GeneralError> {
        let baud_rate: u32 = baud_rate.parse().map_err(GeneralError::Parse)?;
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::ChangeBaud(baud_rate)).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_port_change_data_bits_cmd(&self, data_bits: DataBits) -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::ChangeDataBits(data_bits)).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_port_change_flow_control_cmd(&self,
                                             flow_control: FlowControl)
                                             -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::ChangeFlowControl(flow_control))
            .map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_port_change_stop_bits_cmd(&self, stop_bits: StopBits) -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::ChangeStopBits(stop_bits)).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_port_change_parity_cmd(&self, parity: Parity) -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::ChangeParity(parity)).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_port_change_port_cmd(&self, port_name: String) -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::ChangePort(port_name)).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_port_data_cmd(&self, data: &[u8]) -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::SendData(data.into())).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_port_file_cmd(&self, path: PathBuf) -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::SendFile(path)).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_cancel_file_cmd(&self) -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::CancelSendFile).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_log_to_file_cmd(&self, path: PathBuf) -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::LogToFile(path)).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }

    pub fn send_cancel_log_to_file_cmd(&self) -> Result<(), GeneralError> {
        let tx = &self.to_port_chan_tx;
        // TODO: Remove in favor of impl From for GeneralError
        tx.send(SerialCommand::CancelLogToFile).map_err(|e| GeneralError::Send(e.0))?;
        Ok(())
    }
}