diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ad27e5..488e1ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## [0.3.1] - 2026-04-07 + +### Fixed + +- **Sticky mux reconnect.** Once the bridge connects via mux, all subsequent + reconnects use mux-only mode (`connect_mux_auto`). Previously, each reconnect + re-ran the full mux→USB fallback chain, which could steal the serial port + from the mux during a brief disconnect. + ## [0.3.0] - 2026-04-07 ### Changed diff --git a/Cargo.lock b/Cargo.lock index a1a7438..95fee7c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -906,7 +906,7 @@ dependencies = [ [[package]] name = "donglora-bridge" -version = "0.3.0" +version = "0.3.1" dependencies = [ "anyhow", "blake3", @@ -938,9 +938,9 @@ dependencies = [ [[package]] name = "donglora-client" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1936935d31ab56a8cd70ceecfd6d447f2628a73a123e1cfe37ee4987eab0fa70" +checksum = "343d750fe1e5dad94d4b31e6dbbb05214b39e66f3c94a2ff2a29b0031ffd2826" dependencies = [ "anyhow", "serialport", @@ -1071,9 +1071,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a043dc74da1e37d6afe657061213aa6f425f855399a11d3463c6ecccc4dfda1f" +checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" [[package]] name = "fiat-crypto" @@ -4358,9 +4358,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.25.10+spec-1.1.0" +version = "0.25.11+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a82418ca169e235e6c399a84e395ab6debeb3bc90edc959bf0f48647c6a32d1b" +checksum = "0b59c4d22ed448339746c59b905d24568fcbb3ab65a500494f7b8c3e97739f2b" dependencies = [ "indexmap", "toml_datetime", diff --git a/Cargo.toml b/Cargo.toml index eb204df..3832c8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "donglora-bridge" -version = "0.3.0" +version = "0.3.1" edition = "2024" authors = ["Stephen Waits "] license = "MIT" diff --git a/src/radio.rs b/src/radio.rs index 0bf8b4c..6fe11a1 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -56,15 +56,20 @@ fn radio_loop( let initial_backoff = Duration::from_millis(250); let max_backoff = Duration::from_secs(5); let mut backoff = initial_backoff; + let mut use_mux_only = false; loop { let started = std::time::Instant::now(); - match connect_and_run(&config, port.as_deref(), &event_tx, &mut tx_recv) { - Ok(()) => { + match connect_and_run(&config, port.as_deref(), use_mux_only, &event_tx, &mut tx_recv) { + Ok(_was_mux) => { info!("radio thread exiting"); return; } - Err(e) => { + Err((was_mux, e)) => { + // Session ended with error. Remember transport type for reconnect. + if was_mux { + use_mux_only = true; + } error!("radio error: {e:#}"); let _ = event_tx.blocking_send(RadioEvent::Disconnected); @@ -82,24 +87,47 @@ fn radio_loop( } } +/// Connect to the dongle and run the radio loop. +/// +/// Returns `Ok(was_mux)` on clean exit, `Err((was_mux, error))` on failure. +/// The `was_mux` flag lets the caller know which transport was used so it can +/// stick to the same transport type on reconnect. fn connect_and_run( config: &RadioConfig, port: Option<&str>, + use_mux_only: bool, event_tx: &mpsc::Sender, tx_recv: &mut mpsc::Receiver>, -) -> Result<()> { +) -> std::result::Result { // Long initial timeout: the mux serializes commands through the dongle, // so our `GetConfig` might have to wait behind other clients' commands. let timeout = Duration::from_secs(10); info!("connecting to dongle..."); - let mut client = donglora_client::connect(port, timeout)?; + let mut client = if use_mux_only { + debug!("reconnecting via mux only (previously connected via mux)"); + donglora_client::connect_mux_auto(timeout).map_err(|e| (true, e))? + } else { + donglora_client::connect(port, timeout).map_err(|e| (false, e))? + }; let is_mux = matches!(client.transport(), AnyTransport::Mux(_)); let device = if is_mux { "mux".to_string() } else { port.map_or_else(|| "usb".to_string(), shorten_path) }; info!("[radio] connected and validated: {device}"); + // Run the session. Map Result<()> to include the is_mux flag. + run_session(&mut client, config, is_mux, &device, event_tx, tx_recv).map(|()| is_mux).map_err(|e| (is_mux, e)) +} + +fn run_session( + client: &mut donglora_client::Client, + config: &RadioConfig, + is_mux: bool, + device: &str, + event_tx: &mpsc::Sender, + tx_recv: &mut mpsc::Receiver>, +) -> Result<()> { info!("[radio] negotiating config..."); - let (active_config, config_source) = match negotiate_config(&mut client, config, is_mux) { + let (active_config, config_source) = match negotiate_config(client, config, is_mux) { Ok(result) => { info!("[radio] config negotiated: source={:?}, config={}", result.1, format_radio_config(&result.0)); result @@ -119,7 +147,7 @@ fn connect_and_run( } } - let _ = event_tx.blocking_send(RadioEvent::Connected(active_config, config_source, device)); + let _ = event_tx.blocking_send(RadioEvent::Connected(active_config, config_source, device.to_string())); // Set a short timeout so we can interleave RX and TX. client.transport_mut().set_timeout(Duration::from_millis(100))?;