mavlink_core/connection/udp/config.rs
1use core::fmt::Display;
2
3/// Type of UDP connection
4///
5/// # Example
6///
7/// ```ignore
8/// use mavlink::{Connectable, UdpConfig, UdpMode};
9///
10/// let config = mavlink::UdpConfig::new("0.0.0.0:14552".to_owned(), UdpMode::Udpin);
11/// config
12/// .connect::<mavlink::ardupilotmega::MavMessage>()
13/// .unwrap();
14/// ```
15#[derive(Debug, Clone, Copy)]
16pub enum UdpMode {
17 /// Server connection waiting for a client connection
18 Udpin,
19 /// Client connection connecting to a server
20 Udpout,
21 /// Client connection that is allowed to send to broadcast addresses
22 Udpcast,
23}
24
25/// MAVLink address for a UDP server client or broadcast connection
26#[derive(Debug, Clone)]
27pub struct UdpConfig {
28 pub(crate) address: String,
29 pub(crate) mode: UdpMode,
30}
31
32impl UdpConfig {
33 /// Creates a UDP connection address.
34 ///
35 /// The type of connection depends on the [`UdpMode`]
36 pub fn new(address: String, mode: UdpMode) -> Self {
37 Self { address, mode }
38 }
39}
40
41impl Display for UdpConfig {
42 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43 let mode = match self.mode {
44 UdpMode::Udpin => "udpin",
45 UdpMode::Udpout => "udpout",
46 UdpMode::Udpcast => "udpcast",
47 };
48 write!(f, "{mode}:{}", self.address)
49 }
50}