From d31735c71767b48a57bbb7a92df5f4fb333fc89c Mon Sep 17 00:00:00 2001 From: Federico Lolli <federico.lolli@skywarder.eu> Date: Sun, 13 Apr 2025 16:12:10 +0200 Subject: [PATCH] fixed issue with non-reconfigurable mavlink connection previously changing connection configuration could not be set in place because the wait_for_message thread would have been put in a endless wait --- Cargo.lock | 201 ++++++++++++++++++++++++---------- Cargo.toml | 4 +- src/communication.rs | 12 +- src/communication/ethernet.rs | 14 ++- src/communication/serial.rs | 5 + src/ui/windows/connections.rs | 28 +++-- 6 files changed, 191 insertions(+), 73 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 28368be..4e54fa4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,7 +63,7 @@ dependencies = [ "accesskit_consumer", "hashbrown", "objc2 0.5.2", - "objc2-app-kit", + "objc2-app-kit 0.2.2", "objc2-foundation 0.2.2", ] @@ -187,19 +187,21 @@ checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f" [[package]] name = "arboard" -version = "3.4.1" +version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df099ccb16cd014ff054ac1bf392c67feeef57164b05c42f037cd40f5d4357f4" +checksum = "c1df21f715862ede32a0c525ce2ca4d52626bb0007f8c18b87a384503ac33e70" dependencies = [ "clipboard-win", - "core-graphics", "image", "log", - "objc2 0.5.2", - "objc2-app-kit", - "objc2-foundation 0.2.2", + "objc2 0.6.0", + "objc2-app-kit 0.3.0", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-foundation 0.3.0", "parking_lot", - "windows-sys 0.48.0", + "percent-encoding", + "windows-sys 0.59.0", "x11rb", ] @@ -581,9 +583,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.17" +version = "1.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fcb57c740ae1daf453ae85f16e37396f672b039e00d9d866e07ddb24e328e3a" +checksum = "8e3a13707ac958681c13b39b458c073d0d9bc8a22cb1b2f4c8e55eb72c13f362" dependencies = [ "jobserver", "libc", @@ -731,9 +733,9 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.5.14" +version = "0.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06ba6d68e24814cb8de6bb986db8222d3a027d15872cabc0d18817bc3c0e4471" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" dependencies = [ "crossbeam-utils", ] @@ -777,9 +779,9 @@ checksum = "5c297a1c74b71ae29df00c3e22dd9534821d60eb9af5a0192823fa2acea70c2a" [[package]] name = "deranged" -version = "0.4.1" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28cfac68e08048ae1883171632c2aef3ebc555621ae56fbccce1cbf22dd7f058" +checksum = "9c9e6a11ca8224451684bc0d7d5a7adbf8f2fd6887261a1cfc3c0432f9d4068e" dependencies = [ "powerfmt", ] @@ -911,7 +913,7 @@ dependencies = [ "js-sys", "log", "objc2 0.5.2", - "objc2-app-kit", + "objc2-app-kit 0.2.2", "objc2-foundation 0.2.2", "parking_lot", "percent-encoding", @@ -1176,9 +1178,9 @@ checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" [[package]] name = "errno" -version = "0.3.10" +version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e" dependencies = [ "libc", "windows-sys 0.59.0", @@ -1491,7 +1493,7 @@ dependencies = [ "glutin_wgl_sys", "libloading", "objc2 0.5.2", - "objc2-app-kit", + "objc2-app-kit 0.2.2", "objc2-foundation 0.2.2", "once_cell", "raw-window-handle", @@ -1791,9 +1793,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.8.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3954d50fe15b02142bf25d3b8bdadb634ec3948f103d04ffe3031bc8fe9d7058" +checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" dependencies = [ "equivalent", "hashbrown", @@ -1946,7 +1948,7 @@ checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ "bitflags 2.9.0", "libc", - "redox_syscall 0.5.10", + "redox_syscall 0.5.11", ] [[package]] @@ -1977,9 +1979,9 @@ checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" [[package]] name = "linux-raw-sys" -version = "0.9.3" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe7db12097d22ec582439daf8618b8fdd1a7bef6270e9af3b1ebcd30893cf413" +checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" [[package]] name = "litemap" @@ -2052,7 +2054,7 @@ dependencies = [ [[package]] name = "mavlink-bindgen" version = "0.14.0" -source = "git+https://git.skywarder.eu/avn/swd/mavlink/rust-mavlink.git?rev=da4add3de8243d3b8194b9793677e4c950686ddc#da4add3de8243d3b8194b9793677e4c950686ddc" +source = "git+https://git.skywarder.eu/avn/swd/mavlink/rust-mavlink.git?rev=1b51a25d717993d39004152f215c33f4333e2056#1b51a25d717993d39004152f215c33f4333e2056" dependencies = [ "crc-any", "lazy_static", @@ -2066,7 +2068,7 @@ dependencies = [ [[package]] name = "mavlink-core" version = "0.14.0" -source = "git+https://git.skywarder.eu/avn/swd/mavlink/rust-mavlink.git?rev=b7446436b3c96ca4c40d28b54eeed346e7bf021e#b7446436b3c96ca4c40d28b54eeed346e7bf021e" +source = "git+https://git.skywarder.eu/avn/swd/mavlink/rust-mavlink.git?rev=1b51a25d717993d39004152f215c33f4333e2056#1b51a25d717993d39004152f215c33f4333e2056" dependencies = [ "byteorder", "crc-any", @@ -2122,19 +2124,21 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" [[package]] name = "mime_guess2" -version = "2.0.5" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25a3333bb1609500601edc766a39b4c1772874a4ce26022f4d866854dc020c41" +checksum = "f54028747dfea8e8bf00d3c2d4e83cf023c1accfd5d436335456e9864940cb85" dependencies = [ "mime", + "phf", + "phf_shared", "unicase", ] [[package]] name = "miniz_oxide" -version = "0.8.5" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e3e04debbb59698c15bacbb6d93584a8c0ca9cc3213cb423d31f760d8843ce5" +checksum = "3be647b768db090acb35d5ec5db2b0e1f1de11133ca123b9eacf5137868f892a" dependencies = [ "adler2", "simd-adler32", @@ -2344,6 +2348,18 @@ dependencies = [ "objc2-quartz-core", ] +[[package]] +name = "objc2-app-kit" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5906f93257178e2f7ae069efb89fbd6ee94f0592740b5f8a1512ca498814d0fb" +dependencies = [ + "bitflags 2.9.0", + "objc2 0.6.0", + "objc2-core-graphics", + "objc2-foundation 0.3.0", +] + [[package]] name = "objc2-cloud-kit" version = "0.2.2" @@ -2380,6 +2396,28 @@ dependencies = [ "objc2-foundation 0.2.2", ] +[[package]] +name = "objc2-core-foundation" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daeaf60f25471d26948a1c2f840e3f7d86f4109e3af4e8e4b5cd70c39690d925" +dependencies = [ + "bitflags 2.9.0", + "objc2 0.6.0", +] + +[[package]] +name = "objc2-core-graphics" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dca602628b65356b6513290a21a6405b4d4027b8b250f0b98dddbb28b7de02" +dependencies = [ + "bitflags 2.9.0", + "objc2 0.6.0", + "objc2-core-foundation", + "objc2-io-surface", +] + [[package]] name = "objc2-core-image" version = "0.2.2" @@ -2431,6 +2469,18 @@ checksum = "3a21c6c9014b82c39515db5b396f91645182611c97d24637cf56ac01e5f8d998" dependencies = [ "bitflags 2.9.0", "objc2 0.6.0", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-io-surface" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "161a8b87e32610086e1a7a9e9ec39f84459db7b3a0881c1f16ca5a2605581c19" +dependencies = [ + "bitflags 2.9.0", + "objc2 0.6.0", + "objc2-core-foundation", ] [[package]] @@ -2441,7 +2491,7 @@ checksum = "a1a1ae721c5e35be65f01a03b6d2ac13a54cb4fa70d8a5da293d7b0020261398" dependencies = [ "block2", "objc2 0.5.2", - "objc2-app-kit", + "objc2-app-kit 0.2.2", "objc2-foundation 0.2.2", ] @@ -2604,7 +2654,7 @@ checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ "cfg-if", "libc", - "redox_syscall 0.5.10", + "redox_syscall 0.5.11", "smallvec", "windows-targets 0.52.6", ] @@ -2621,6 +2671,50 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "phf" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078" +dependencies = [ + "phf_macros", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_macros" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f84ac04429c13a7ff43785d75ad27569f2951ce0ffd30a3321230db2fc727216" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn 2.0.100", + "unicase", +] + +[[package]] +name = "phf_shared" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" +dependencies = [ + "siphasher 1.0.1", + "unicase", +] + [[package]] name = "pico-args" version = "0.5.0" @@ -2853,9 +2947,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.10" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b8c0c260b63a8219631167be35e6a988e9554dbd323f8bd08439c8ed1302bd1" +checksum = "d2f103c6d277498fbceb16e84d317e2a400f160f46904d5f5410848c829511a3" dependencies = [ "bitflags 2.9.0", ] @@ -3004,7 +3098,7 @@ dependencies = [ "bitflags 2.9.0", "errno", "libc", - "linux-raw-sys 0.9.3", + "linux-raw-sys 0.9.4", "windows-sys 0.59.0", ] @@ -3254,10 +3348,16 @@ version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" +[[package]] +name = "siphasher" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" + [[package]] name = "skyward_mavlink" version = "0.1.1" -source = "git+https://git.skywarder.eu/avn/swd/mavlink/mavlink-skyward-lib.git?rev=03d37888f7b5a84b5032ca1af392a16da7f39df2#03d37888f7b5a84b5032ca1af392a16da7f39df2" +source = "git+https://git.skywarder.eu/avn/swd/mavlink/mavlink-skyward-lib.git?rev=b7233f7b2472415e0bbfb2fb26ca30d3a9f985c3#b7233f7b2472415e0bbfb2fb26ca30d3a9f985c3" dependencies = [ "bitflags 2.9.0", "mavlink-bindgen", @@ -3290,9 +3390,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.14.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" +checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9" [[package]] name = "smithay-client-toolkit" @@ -3404,7 +3504,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e44e288cd960318917cbd540340968b90becc8bc81f171345d706e7a89d9d70" dependencies = [ "kurbo", - "siphasher", + "siphasher 0.3.11", ] [[package]] @@ -3832,7 +3932,7 @@ dependencies = [ "log", "roxmltree", "simplecss", - "siphasher", + "siphasher 0.3.11", "svgtypes", "usvg-tree", ] @@ -4173,9 +4273,9 @@ dependencies = [ [[package]] name = "wgpu-hal" -version = "24.0.2" +version = "24.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4317a17171dc20e6577bf606796794580accae0716a69edbc7388c86a3ec9f23" +checksum = "f112f464674ca69f3533248508ee30cb84c67cf06c25ff6800685f5e0294e259" dependencies = [ "android_system_properties", "arrayvec", @@ -4328,15 +4428,6 @@ dependencies = [ "windows-targets 0.42.2", ] -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets 0.48.5", -] - [[package]] name = "windows-sys" version = "0.52.0" @@ -4557,7 +4648,7 @@ dependencies = [ "memmap2", "ndk", "objc2 0.5.2", - "objc2-app-kit", + "objc2-app-kit 0.2.2", "objc2-foundation 0.2.2", "objc2-ui-kit", "orbclient", @@ -4587,9 +4678,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.7.4" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e97b544156e9bebe1a0ffbc03484fc1ffe3100cbce3ffb17eac35f7cdd7ab36" +checksum = "63d3fcd9bba44b03821e7d699eeee959f3126dcc4aa8e4ae18ec617c2a5cea10" dependencies = [ "memchr", ] @@ -4684,9 +4775,9 @@ checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56" [[package]] name = "xml-rs" -version = "0.8.25" +version = "0.8.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5b940ebc25896e71dd073bad2dbaa2abfe97b0a391415e22ad1326d9c54e3c4" +checksum = "a62ce76d9b56901b19a74f19431b0d8b3bc7ca4ad685a746dfd78ca8f4fc6bda" [[package]] name = "xmlwriter" diff --git a/Cargo.toml b/Cargo.toml index b049c64..3bf0105 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,12 +37,12 @@ tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] } [dependencies.skyward_mavlink] git = "https://git.skywarder.eu/avn/swd/mavlink/mavlink-skyward-lib.git" -rev = "03d37888f7b5a84b5032ca1af392a16da7f39df2" +rev = "b7233f7b2472415e0bbfb2fb26ca30d3a9f985c3" features = ["reflection", "orion", "serde"] [dependencies.mavlink-bindgen] git = "https://git.skywarder.eu/avn/swd/mavlink/rust-mavlink.git" -rev = "da4add3de8243d3b8194b9793677e4c950686ddc" +rev = "1b51a25d717993d39004152f215c33f4333e2056" features = ["serde"] [features] diff --git a/src/communication.rs b/src/communication.rs index cf0bbcc..153f1ac 100644 --- a/src/communication.rs +++ b/src/communication.rs @@ -29,6 +29,7 @@ pub(super) type BoxedConnection = Box<dyn MavConnection<MavMessage> + Send + Syn mod sealed { use std::{ + io::ErrorKind, num::NonZeroUsize, sync::{ Arc, @@ -93,10 +94,15 @@ mod sealed { tx.send(msg) .map_err(|_| CommunicationError::ConnectionClosed)?; } + // Ignore timeouts (they are used to poll the connection and check if this thread should stop) Err(MessageReadError::Io(e)) => { - tracing::error!("Failed to read message: {e:#?}"); - running_flag.store(false, Ordering::Relaxed); - return Err(CommunicationError::Io(e)); + if e.kind() != ErrorKind::WouldBlock + && e.kind() != ErrorKind::TimedOut + { + tracing::error!("Failed to read message: {e:#?}"); + running_flag.store(false, Ordering::Relaxed); + return Err(CommunicationError::Io(e)); + } } Err(MessageReadError::Parse(e)) => { tracing::error!("Failed to read message: {e:#?}"); diff --git a/src/communication/ethernet.rs b/src/communication/ethernet.rs index 00331ed..64423e9 100644 --- a/src/communication/ethernet.rs +++ b/src/communication/ethernet.rs @@ -3,6 +3,8 @@ //! Provides functionality to connect via Ethernet using UDP, allowing message //! transmission and reception over a network. +use std::time::Duration; + use skyward_mavlink::mavlink::{ self, error::{MessageReadError, MessageWriteError}, @@ -19,7 +21,8 @@ use super::{ /// Configuration for an Ethernet connection. #[derive(Debug, Clone)] pub struct EthernetConfiguration { - pub port: u16, + pub send_port: u16, + pub receive_port: u16, } impl Connectable for EthernetConfiguration { @@ -28,13 +31,16 @@ impl Connectable for EthernetConfiguration { /// Binds to the specified UDP port to create a network connection. #[profiling::function] fn connect(&self) -> Result<Self::Connected, ConnectionError> { - let incoming_addr = format!("udpin:0.0.0.0:{}", self.port); - let outgoing_addr = format!("udpcast:255.255.255.255:{}", self.port); + let incoming_addr = format!("udpin:0.0.0.0:{}", self.receive_port); + let outgoing_addr = format!("udpcast:255.255.255.255:{}", self.send_port); let mut incoming_conn: BoxedConnection = mavlink::connect(&incoming_addr)?; let mut outgoing_conn: BoxedConnection = mavlink::connect(&outgoing_addr)?; incoming_conn.set_protocol_version(MavlinkVersion::V1); + incoming_conn.set_read_timeout(Some(Duration::from_millis(100)))?; outgoing_conn.set_protocol_version(MavlinkVersion::V1); - debug!("Ethernet connections set up on port {}", self.port); + outgoing_conn.set_write_timeout(Some(Duration::from_millis(100)))?; + debug!("Receiving Ethernet set up on port {}", self.receive_port); + debug!("Sending Ethernet set up on port {}", self.send_port); Ok(EthernetTransceiver { incoming_conn, outgoing_conn, diff --git a/src/communication/serial.rs b/src/communication/serial.rs index a27efdb..45ac247 100644 --- a/src/communication/serial.rs +++ b/src/communication/serial.rs @@ -3,6 +3,8 @@ //! Provides functions for listing USB serial ports, finding a STM32 port, //! and handling serial connections including message transmission and reception. +use std::time::Duration; + use serialport::{SerialPortInfo, SerialPortType}; use skyward_mavlink::mavlink::{ self, @@ -101,6 +103,9 @@ impl Connectable for SerialConfiguration { let serial_edpoint = format!("serial:{}:{}", self.port_name, self.baud_rate); let mut mav_connection: BoxedConnection = mavlink::connect(&serial_edpoint)?; mav_connection.set_protocol_version(MavlinkVersion::V1); + // these two are redundant right now + mav_connection.set_read_timeout(Some(Duration::from_millis(100)))?; + mav_connection.set_write_timeout(Some(Duration::from_millis(100)))?; debug!( "Connected to serial port {} with baud rate {}", self.port_name, self.baud_rate diff --git a/src/ui/windows/connections.rs b/src/ui/windows/connections.rs index 30ffe5e..6b4e960 100644 --- a/src/ui/windows/connections.rs +++ b/src/ui/windows/connections.rs @@ -73,15 +73,24 @@ impl ConnectionsWindow { } match connection_config { - ConnectionConfig::Ethernet(EthernetConfiguration { port: recv_port }) => { - egui::Grid::new("grid") - .num_columns(2) - .spacing([10.0, 5.0]) - .show(ui, |ui| { - ui.label("Ethernet Port:"); - ui.add(egui::DragValue::new(recv_port).range(0..=65535).speed(10)); - ui.end_row(); + ConnectionConfig::Ethernet(EthernetConfiguration { + send_port, + receive_port, + }) => { + ui.vertical(|ui| { + ui.horizontal(|ui| { + ui.label("Send Port:"); + ui.add(egui::DragValue::new(send_port).range(0..=65535).speed(10)); + }); + ui.horizontal(|ui| { + ui.label("Receive Port:"); + ui.add( + egui::DragValue::new(receive_port) + .range(0..=65535) + .speed(10), + ); }); + }); } ConnectionConfig::Serial(opt) => { egui::Grid::new("grid") @@ -180,7 +189,8 @@ pub enum ConnectionConfig { fn default_ethernet() -> EthernetConfiguration { EthernetConfiguration { - port: DEFAULT_ETHERNET_PORT, + send_port: DEFAULT_ETHERNET_PORT, + receive_port: DEFAULT_ETHERNET_PORT, } } -- GitLab