mavlink_core/connection/tcp/
config.rs

1use core::fmt::Display;
2
3/// Type of TCP connection
4#[derive(Debug, Clone, Copy)]
5pub enum TcpMode {
6    /// Connection will open a TCP server that binds to the provided address
7    TcpIn,
8    /// Connection will connect to the provided TCP server address
9    TcpOut,
10}
11
12/// MAVLink connection address for a TCP server or client
13///
14/// # Example
15///
16/// ```ignore
17/// use mavlink::{Connectable, TcpConfig, TcpMode};
18///
19/// let config = TcpConfig::new("0.0.0.0:14551".to_owned(), false);
20/// config.connect::<mavlink::ardupilotmega::MavMessage>();
21/// ```
22#[derive(Debug, Clone)]
23pub struct TcpConfig {
24    pub(crate) address: String,
25    pub(crate) mode: TcpMode,
26}
27
28impl TcpConfig {
29    /// Creates a TCP connection address.
30    pub fn new(address: String, mode: TcpMode) -> Self {
31        Self { address, mode }
32    }
33}
34impl Display for TcpConfig {
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        match self.mode {
37            TcpMode::TcpIn => write!(f, "tcpin:{}", self.address),
38            TcpMode::TcpOut => write!(f, "tcpout:{}", self.address),
39        }
40    }
41}