Skip to main content

serialplugin_core/api/
lines.rs

1//! Which ports have real modem control lines.
2
3use crate::api::SerialManager;
4use crate::cmux::parse_mux_path;
5use crate::port::tcp::TCP_SCHEME;
6
7impl SerialManager {
8    /// `false` when `path` has no real RTS/DTR/CTS/DSR/RI/DCD: `tcp://` bridges and
9    /// `ble://` UARTs, where the modem-line setters still succeed (the value is stored
10    /// locally) and the getters return fixed values, and CMUX channels
11    /// (`path#dlci=N`), where line calls fail (the lines belong to the physical
12    /// port). Check this before relying on hardware flow control or line signalling.
13    /// Local serial ports return `true`. Needs no open port.
14    pub fn supports_modem_lines(&self, path: &str) -> bool {
15        !(path.starts_with(TCP_SCHEME)
16            || path.starts_with("ble://")
17            || parse_mux_path(path).is_some())
18    }
19}
20
21#[cfg(test)]
22mod tests {
23    use crate::api::SerialManager;
24
25    #[test]
26    fn bridges_have_no_modem_lines() {
27        let serial = SerialManager::new();
28        assert!(!serial.supports_modem_lines("tcp://10.0.0.5:4000"));
29        assert!(!serial.supports_modem_lines("ble://AA:BB:CC:DD:EE:FF"));
30        assert!(!serial.supports_modem_lines("tcp://10.0.0.5:4000#dlci=2"));
31        // Line calls fail on CMUX channels (the lines belong to the physical port).
32        assert!(!serial.supports_modem_lines("/dev/ttyUSB0#dlci=2"));
33        assert!(serial.supports_modem_lines("/dev/ttyUSB0"));
34        assert!(serial.supports_modem_lines("COM3"));
35    }
36}