mavlink_core/connection/udp/config.rs
1use core::fmt::Display;
2use std::time::Duration;
3
4/// Type of UDP connection
5///
6/// # Example
7///
8/// ```ignore
9/// use mavlink::{Connectable, UdpConfig, UdpMode};
10///
11/// let config = mavlink::UdpConfig::new("0.0.0.0:14552".to_owned(), UdpMode::Udpin);
12/// config
13/// .connect::<mavlink::ardupilotmega::MavMessage>()
14/// .unwrap();
15/// ```
16#[derive(Debug, Clone, Copy)]
17pub enum UdpMode {
18 /// Server connection waiting for a client connection
19 Udpin,
20 /// Client connection connecting to a server
21 Udpout,
22 /// Client connection that is allowed to send to broadcast addresses
23 Udpcast,
24}
25
26/// MAVLink address for a UDP server client or broadcast connection
27#[derive(Debug, Clone)]
28pub struct UdpConfig {
29 pub(crate) address: String,
30 pub(crate) mode: UdpMode,
31 pub(crate) read_timeout: Option<Duration>,
32}
33
34impl UdpConfig {
35 /// Creates a UDP connection address.
36 ///
37 /// The type of connection depends on the [`UdpMode`]
38 pub fn new(address: String, mode: UdpMode) -> Self {
39 Self {
40 address,
41 mode,
42 read_timeout: None,
43 }
44 }
45
46 /// Sets the read timeout on the UDP socket.
47 ///
48 /// When set, `recv()` and `recv_raw()` will return an error after the
49 /// specified duration instead of blocking indefinitely. This is useful
50 /// for implementing graceful shutdown.
51 pub fn read_timeout(mut self, timeout: Duration) -> Self {
52 self.read_timeout = Some(timeout);
53 self
54 }
55}
56
57impl Display for UdpConfig {
58 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
59 let mode = match self.mode {
60 UdpMode::Udpin => "udpin",
61 UdpMode::Udpout => "udpout",
62 UdpMode::Udpcast => "udpcast",
63 };
64 write!(f, "{mode}:{}", self.address)
65 }
66}