Skip to content

Commit a1279c8

Browse files
committed
Three levels of reconnection strictness checks instead of two!
1 parent ae6ed43 commit a1279c8

7 files changed

Lines changed: 88 additions & 58 deletions

File tree

src/app.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ use crate::{
4646
keybinds::{Action, BaseAction, BuiltinAction, Keybinds, PortAction, ShowPopupAction},
4747
notifications::{EMERGE_TIME, EXPAND_TIME, EXPIRE_TIME, Notifications, PAUSE_AND_SHOW_TIME},
4848
serial::{
49-
DeserializedUsb, PrintablePortInfo, ReconnectType, Reconnections, SerialDisconnectReason,
50-
SerialEvent,
49+
DeserializedUsb, PrintablePortInfo, ReconnectType, ReconnectionStrictness,
50+
SerialDisconnectReason, SerialEvent,
5151
handle::{BlockingCommandError, SerialHandle},
5252
port_status::InnerPortStatus,
5353
worker::MOCK_PORT_NAME,
@@ -792,11 +792,16 @@ impl App {
792792
}
793793
SerialDisconnectReason::Error(error) => {
794794
error!("Serial worker reported error on disconnect! {error}");
795-
let reconnect_text = match &self.settings.serial.reconnections {
796-
Reconnections::Disabled => "Not attempting to reconnect",
797-
Reconnections::LooseChecks => "Attempting to reconnect (loose checks)",
798-
Reconnections::StrictChecks => {
799-
"Attempting to reconnect (strict checks)"
795+
let reconnect_text = match &self.settings.serial.reconnection_strictness {
796+
ReconnectionStrictness::Disabled => "Not attempting to reconnect",
797+
ReconnectionStrictness::Low => {
798+
"Attempting to reconnect (low strictness checks)"
799+
}
800+
ReconnectionStrictness::Med => {
801+
"Attempting to reconnect (med strictness checks)"
802+
}
803+
ReconnectionStrictness::High => {
804+
"Attempting to reconnect (high strictness checks)"
800805
}
801806
};
802807
self.notifs.notify_str(
@@ -860,8 +865,12 @@ impl App {
860865

861866
let port_status = &self.serial.port_status.load().status();
862867

863-
let reconnections_allowed =
864-
self.serial.port_settings.load().reconnections.allowed();
868+
let reconnections_allowed = self
869+
.serial
870+
.port_settings
871+
.load()
872+
.reconnection_strictness
873+
.allowed();
865874
if !port_status.is_connected()
866875
&& !port_status.is_lent_out()
867876
&& reconnections_allowed
@@ -1733,13 +1742,17 @@ impl App {
17331742
A::Port(PortAction::DeassertRts) => {
17341743
self.serial.write_signals(None, Some(false))?;
17351744
}
1736-
A::Port(PortAction::AttemptReconnectStrict) => {
1745+
A::Port(PortAction::AttemptReconnectStrictHigh) => {
1746+
self.serial
1747+
.request_reconnect(Some(ReconnectionStrictness::High))?;
1748+
}
1749+
A::Port(PortAction::AttemptReconnectStrictMed) => {
17371750
self.serial
1738-
.request_reconnect(Some(Reconnections::StrictChecks))?;
1751+
.request_reconnect(Some(ReconnectionStrictness::Med))?;
17391752
}
1740-
A::Port(PortAction::AttemptReconnectLoose) => {
1753+
A::Port(PortAction::AttemptReconnectStrictLow) => {
17411754
self.serial
1742-
.request_reconnect(Some(Reconnections::LooseChecks))?;
1755+
.request_reconnect(Some(ReconnectionStrictness::Low))?;
17431756
}
17441757
A::Base(BaseAction::ToggleTextwrap) => {
17451758
let state = pretty_bool(self.settings.rendering.wrap_text.flip());
@@ -1996,7 +2009,7 @@ impl App {
19962009
// and if auto-reconnections is disabled.
19972010
// (when auto-reconns. are enabled, the normal Disconnect prompt
19982011
// is supposed to show to act as a pause for auto-reconnections)
1999-
&& !port_settings_guard.reconnections.allowed())
2012+
&& !port_settings_guard.reconnection_strictness.allowed())
20002013
}
20012014
// fn tab_pressed(&mut self) {}
20022015
fn esc_pressed(&mut self) {
@@ -2877,14 +2890,14 @@ impl App {
28772890
self.notifs
28782891
.notify_str("Attempting to reconnect! (Loose Checks)", Color::Yellow);
28792892
self.serial
2880-
.request_reconnect(Some(Reconnections::LooseChecks))?;
2893+
.request_reconnect(Some(ReconnectionStrictness::Low))?;
28812894
}
28822895
AttemptReconnectPrompt::AttemptReconnect => {
28832896
self.repeating_line_flip.flip();
28842897
self.notifs
28852898
.notify_str("Attempting to reconnect! (Strict Checks)", Color::Yellow);
28862899
self.serial
2887-
.request_reconnect(Some(Reconnections::StrictChecks))?;
2900+
.request_reconnect(Some(ReconnectionStrictness::High))?;
28882901
}
28892902
AttemptReconnectPrompt::Cancel => self.dismiss_popup(),
28902903
AttemptReconnectPrompt::OpenPortSettings => {
@@ -3227,7 +3240,7 @@ impl App {
32273240
}
32283241
Popup::DisconnectPrompt => {
32293242
let port_state = { self.serial.port_status.load().status() };
3230-
let reconns_paused = if self.settings.serial.reconnections.allowed()
3243+
let reconns_paused = if self.settings.serial.reconnection_strictness.allowed()
32313244
&& port_state.is_premature_disconnect()
32323245
{
32333246
Some("(Auto-reconnections paused while open)")

src/keybinds.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,19 @@ pub enum PortAction {
173173
DeassertDtr,
174174

175175
/// Attempt to reconnect to device, must match USB info if applicable.
176-
AttemptReconnectStrict,
176+
AttemptReconnectStrictHigh,
177+
AttemptReconnectStrictMed,
177178
/// Attempt to reconnect to device, best-effort.
178-
AttemptReconnectLoose,
179+
AttemptReconnectStrictLow,
179180
}
180181

181182
impl RequiresPort for PortAction {
182183
fn requires_connection(&self) -> bool {
183184
match self {
184185
// Can't require a connection for the method to revitalize one.
185-
Self::AttemptReconnectLoose | Self::AttemptReconnectStrict => false,
186+
Self::AttemptReconnectStrictHigh
187+
| Self::AttemptReconnectStrictMed
188+
| Self::AttemptReconnectStrictLow => false,
186189
_ => true,
187190
}
188191
}

src/serial/handle.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tracing::{debug, error};
99

1010
use crate::{
1111
app::Event,
12-
serial::{Reconnections, port_status::PortStatus, worker::WorkerError},
12+
serial::{ReconnectionStrictness, port_status::PortStatus, worker::WorkerError},
1313
settings::{Ignored, PortSettings},
1414
};
1515

@@ -29,7 +29,7 @@ pub enum SerialWorkerCommand {
2929
result_tx: Sender<Result<(), super::worker::WorkerError>>,
3030
},
3131
PortCommand(PortCommand),
32-
RequestReconnect(Option<Reconnections>),
32+
RequestReconnect(Option<ReconnectionStrictness>),
3333
Disconnect {
3434
user_wants_break: bool,
3535
},
@@ -226,7 +226,10 @@ impl SerialHandle {
226226
}
227227
}
228228
/// Non-blocking request for the serial worker to attempt to reconnect to the "current" device
229-
pub fn request_reconnect(&self, strictness_opt: Option<Reconnections>) -> HandleResult<()> {
229+
pub fn request_reconnect(
230+
&self,
231+
strictness_opt: Option<ReconnectionStrictness>,
232+
) -> HandleResult<()> {
230233
self.command_tx
231234
.send(SerialWorkerCommand::RequestReconnect(strictness_opt))?;
232235
Ok(())

src/serial/mod.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,34 @@ impl From<SerialEvent> for Event {
7777
)]
7878
#[strum(serialize_all = "title_case")]
7979
/// Allowance level of Auto-Reconnections
80-
pub enum Reconnections {
80+
pub enum ReconnectionStrictness {
8181
/// No auto reconnections
8282
Disabled,
83+
84+
/// Also known as "Strict"
8385
/// Will only reconnect to devices that either:
8486
/// 1. Match the last device exactly (Port Name + Port Type) -> PerfectMatch
8587
/// 2. Match the USB characteristics exactly (PID, VID, Serial, and the rest) -> UsbStrict
86-
StrictChecks,
87-
/// also known as "Best-Effort"
88+
#[serde(alias = "StrictChecks")]
89+
High,
90+
91+
/// Also known as "Loose"
8892
/// Will first try the Strict Checks and if those fail, will try to connect to devices that:
8993
/// 3. Match the USB PID and VID of the last device -> UsbLoose
94+
Med,
95+
96+
/// Also known as "Best-Effort"
97+
/// Will first try the Strict+Loose Checks and if those fail, will try to connect to devices that:
9098
/// 4. Any port at the same path of the last device -> LastDitch
91-
LooseChecks,
99+
#[serde(alias = "LooseChecks")]
100+
Low,
92101
}
93102

94-
impl Reconnections {
103+
impl ReconnectionStrictness {
95104
pub fn allowed(&self) -> bool {
96105
match self {
97-
Reconnections::Disabled => false,
98-
Reconnections::LooseChecks | Reconnections::StrictChecks => true,
106+
ReconnectionStrictness::Disabled => false,
107+
_ => true,
99108
}
100109
}
101110
}

src/serial/worker.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
};
2020

2121
use super::{
22-
ReconnectType, Reconnections,
22+
ReconnectType, ReconnectionStrictness,
2323
handle::{PortCommand, SerialWorkerCommand},
2424
};
2525

@@ -361,7 +361,7 @@ impl SerialWorker {
361361
} => {
362362
// self.port_status = SerialStatus::idle();
363363

364-
let settings = self.shared_settings.load();
364+
// let settings = self.shared_settings.load();
365365

366366
let previous_status = { self.shared_status.load().as_ref().clone() };
367367

@@ -539,7 +539,7 @@ impl SerialWorker {
539539
/// not that reconnection was successful.
540540
fn attempt_reconnect(
541541
&mut self,
542-
strictness_opt: Option<Reconnections>,
542+
strictness_opt: Option<ReconnectionStrictness>,
543543
) -> Result<(), WorkerError> {
544544
let port_available = self.port.is_available();
545545
#[cfg(feature = "espflash")]
@@ -549,10 +549,10 @@ impl SerialWorker {
549549
return Ok(());
550550
}
551551

552-
let reconnections =
553-
strictness_opt.unwrap_or_else(|| self.shared_settings.load().reconnections.clone());
552+
let reconnections = strictness_opt
553+
.unwrap_or_else(|| self.shared_settings.load().reconnection_strictness.clone());
554554

555-
if reconnections == Reconnections::Disabled {
555+
if reconnections == ReconnectionStrictness::Disabled {
556556
warn!("Got request to reconnect when reconnections are disabled!");
557557
return Ok(());
558558
}
@@ -602,21 +602,23 @@ impl SerialWorker {
602602
};
603603

604604
// Loose check
605-
if reconnections == Reconnections::LooseChecks
606-
&& let Some(port) = current_ports
607-
.iter()
608-
// Filtering out ports that didn't change across scans
609-
.filter(|p| !self.scan_snapshot.contains(p))
610-
// Trying to find another USB device with *just* matching USB PID & VID
611-
// Use cases: Some devices seem to change their Serial # arbitrarily?
612-
// - And for interfacing with several identical devices (one at a time) without reconnecting via TUI
613-
// Needs a toggle with Strict/Loose options, as the extra behavior isn't always desirable.
614-
.find(|p| match &p.port_type {
615-
SerialPortType::UsbPort(usb) => {
616-
usb.vid == desired_usb.vid && usb.pid == desired_usb.pid
617-
}
618-
_ => false,
619-
})
605+
if matches!(
606+
reconnections,
607+
ReconnectionStrictness::Med | ReconnectionStrictness::Low
608+
) && let Some(port) = current_ports
609+
.iter()
610+
// Filtering out ports that didn't change across scans
611+
.filter(|p| !self.scan_snapshot.contains(p))
612+
// Trying to find another USB device with *just* matching USB PID & VID
613+
// Use cases: Some devices seem to change their Serial # arbitrarily?
614+
// - And for interfacing with several identical devices (one at a time) without reconnecting via TUI
615+
// Needs a toggle with Strict/Loose options, as the extra behavior isn't always desirable.
616+
.find(|p| match &p.port_type {
617+
SerialPortType::UsbPort(usb) => {
618+
usb.vid == desired_usb.vid && usb.pid == desired_usb.pid
619+
}
620+
_ => false,
621+
})
620622
{
621623
info!(
622624
"[NON-STRICT] Connecting to similar USB device with port: {}",
@@ -629,7 +631,7 @@ impl SerialWorker {
629631
}
630632

631633
// Loose check
632-
if reconnections == Reconnections::LooseChecks
634+
if matches!(reconnections, ReconnectionStrictness::Low)
633635
// Last ditch effort, just try to connect to the same port_name if it's present.
634636
&& let Some(port) = current_ports
635637
.iter()

src/settings/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use strum::VariantArray;
1919
use crate::{
2020
app::{COMMON_BAUD_TRUNC, DEFAULT_BAUD},
2121
buffer::UserEcho,
22-
serial::{DeserializedUsb, Reconnections, SignalAssertion},
22+
serial::{DeserializedUsb, ReconnectionStrictness, SignalAssertion},
2323
};
2424

2525
pub mod ser;
@@ -552,9 +552,9 @@ pub struct PortSettings {
552552
#[table(rename = "Limit TX Speed")]
553553
pub limit_tx_speed: bool,
554554

555-
/// Enable reconnections. Strict checks USB PID+VID+Serial#. Loose checks for any similar USB device/COM port.
556-
#[table(values = Reconnections::VARIANTS)]
557-
pub reconnections: Reconnections,
555+
/// Enable reconnections with checks. High checks USB PID+VID+Serial#, Med checks USB PID+VID, and Low checks for *any* similar USB device or COM port.
556+
#[table(values = ReconnectionStrictness::VARIANTS)]
557+
pub reconnection_strictness: ReconnectionStrictness,
558558

559559
/// Line endings for RX'd data.
560560
#[table(display = ["\\n", "\\r", "\\r\\n", "None"])]
@@ -607,7 +607,7 @@ impl Default for PortSettings {
607607
dtr_on_reconnect: SignalAssertion::InheritConnect,
608608
rts_on_reconnect: SignalAssertion::InheritConnect,
609609
limit_tx_speed: true,
610-
reconnections: Reconnections::LooseChecks,
610+
reconnection_strictness: ReconnectionStrictness::Med,
611611
}
612612
}
613613
}

src/tui/prompts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::str::FromStr;
22

3-
use crokey::{KeyCombination, crossterm::event::KeyCode};
3+
use crokey::crossterm::event::KeyCode;
44
use ratatui::{
55
Frame,
66
layout::{Alignment, Constraint, Rect},

0 commit comments

Comments
 (0)