Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Store modem info in options.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger committed Feb 28, 2024
1 parent 683e0ae commit 2e3ab26
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 13 deletions.
25 changes: 15 additions & 10 deletions src/com/modem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
use crate::Modem;

use super::{Com, OpenConnectionData, TermComResult};
use dark_light::Mode;
use serial::{prelude::*, unix::TTYPort};
use std::{
io::{self, ErrorKind, Read, Write},
net::{TcpStream, ToSocketAddrs},
time::Duration,
};
use serial::prelude::*;
use std::io::{Read, Write};

pub struct ComModemImpl {
modem: Modem,
Expand Down Expand Up @@ -50,9 +45,19 @@ impl Com for ComModemImpl {

fn read_data(&mut self) -> TermComResult<Option<Vec<u8>>> {
let mut buf: Vec<u8> = (0..255).collect();
let size = self.port.read(&mut buf[..])?;
buf.truncate(size);
Ok(Some(buf))
match self.port.read(&mut buf[..]) {
Ok(size) => {
buf.truncate(size);
Ok(Some(buf))
}
Err(e) => {
if e.kind() == std::io::ErrorKind::TimedOut {
Ok(None)
} else {
Err(e.into())
}
}
}
}

fn send(&mut self, buf: &[u8]) -> TermComResult<usize> {
Expand Down
116 changes: 115 additions & 1 deletion src/data/modem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use serial::{prelude::*, CharSize, FlowControl, StopBits};
use std::io::Write;

use serial::{CharSize, FlowControl, StopBits};

use crate::TerminalResult;

#[derive(Clone, Debug, PartialEq)]
pub struct Modem {
Expand All @@ -14,6 +18,116 @@ pub struct Modem {
pub init_string: String,
pub dial_string: String,
}
impl Modem {
pub(crate) fn write_modem_settings(&self, file: &mut std::fs::File) -> TerminalResult<()> {
// currently unused
file.write_all("name = \"Modem 1\"\n".to_string().as_bytes())?;

file.write_all(format!("device = \"{}\"\n", self.device).as_bytes())?;
file.write_all(format!("baud_rate = {}\n", self.baud_rate).as_bytes())?;
let cs = match self.char_size {
CharSize::Bits5 => 5,
CharSize::Bits6 => 6,
CharSize::Bits7 => 7,
CharSize::Bits8 => 8,
};
file.write_all(format!("char_size = {cs}\n").as_bytes())?;

let cs = match self.stop_bits {
StopBits::Stop1 => 1,
StopBits::Stop2 => 2,
};
file.write_all(format!("stop_bits = {cs}\n").as_bytes())?;

let cs = match self.parity {
serial::Parity::ParityNone => "None",
serial::Parity::ParityOdd => "Odd",
serial::Parity::ParityEven => "Even",
};
file.write_all(format!("parity = \"{cs}\"\n").as_bytes())?;

let cs = match self.flow_control {
FlowControl::FlowNone => "None",
FlowControl::FlowSoftware => "Software",
FlowControl::FlowHardware => "Hardware",
};
file.write_all(format!("flow_control = \"{cs}\"\n").as_bytes())?;
file.write_all(format!("init_string = \"{}\"\n", self.init_string).as_bytes())?;
file.write_all(format!("dial_string = \"{}\"\n", self.dial_string).as_bytes())?;

Ok(())
}

pub(crate) fn from_table(table: &toml::map::Map<String, toml::Value>) -> Modem {
let mut result = Modem::default();
for (k, v) in table {
match k.as_str() {
"device" => {
if let toml::Value::String(s) = v {
result.device = s.to_string();
}
}
"baud_rate" => {
if let toml::Value::Integer(i) = v {
result.baud_rate = *i as usize;
}
}
"char_size" => {
if let toml::Value::Integer(i) = v {
result.char_size = match i {
5 => CharSize::Bits5,
6 => CharSize::Bits6,
7 => CharSize::Bits7,
//8 => CharSize::Bits8,
_ => CharSize::Bits8,
};
}
}
"stop_bits" => {
if let toml::Value::Integer(i) = v {
result.stop_bits = match i {
//1 => StopBits::Stop1,
2 => StopBits::Stop2,
_ => StopBits::Stop1,
};
}
}
"parity" => {
if let toml::Value::String(s) = v {
result.parity = match s.as_str() {
//"None" => serial::Parity::ParityNone,
"Odd" => serial::Parity::ParityOdd,
"Even" => serial::Parity::ParityEven,
_ => serial::Parity::ParityNone,
};
}
}
"flow_control" => {
if let toml::Value::String(s) = v {
result.flow_control = match s.as_str() {
// "None" => FlowControl::FlowNone,
"Software" => FlowControl::FlowSoftware,
"Hardware" => FlowControl::FlowHardware,
_ => FlowControl::FlowNone,
};
}
}
"init_string" => {
if let toml::Value::String(s) = v {
result.init_string = s.to_string();
}
}
"dial_string" => {
if let toml::Value::String(s) = v {
result.dial_string = s.to_string();
}
}
_ => {}
}
}
result
}
}

impl Default for Modem {
fn default() -> Self {
Expand Down
13 changes: 13 additions & 0 deletions src/data/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ impl Options {

write_keybindings(&mut file, &self.bind)?;

file.write_all("[[modem]]\n".to_string().as_bytes())?;
self.modem.write_modem_settings(&mut file)?;

file.flush()?;

// move temp file to the real file
Expand Down Expand Up @@ -516,6 +519,16 @@ fn parse_value(options: &mut Options, value: &Value) {
options.capture_filename = b.clone();
}
}*/
"modem" => {
if let Value::Array(array) = v {
for v in array {
if let Value::Table(b) = v {
options.modem = Modem::from_table(b);
break;
}
}
}
}
_ => {}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/ui/terminal_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ impl MainWindow {
if matches!(self.get_mode(), MainWindowMode::ShowTerminal) && ui.is_enabled() && !self.show_find_dialog {
let events = ui.input(|i| i.events.clone());
for e in events {
println!("{e:?}");
match e {
egui::Event::PointerButton {
button: PointerButton::Middle,
Expand Down Expand Up @@ -394,7 +393,6 @@ impl MainWindow {
..
} => {
let key = if let Some(key) = physical_key { key } else { key };
println!("Key: {key:?}, modifiers:{modifiers:?}");
self.handle_key_press(ui, &response, key, modifiers);
}
_ => {}
Expand Down

0 comments on commit 2e3ab26

Please sign in to comment.