Skip to main content

rust_mqtt/config/
mod.rs

1//! Contains configuration primitives and accumulations for server and client configuration.
2
3mod client;
4mod server;
5mod shared;
6
7use core::num::NonZero;
8
9pub use client::Config as ClientConfig;
10pub use server::Config as ServerConfig;
11pub use shared::Config as SharedConfig;
12
13/// Keep alive mechanism within a connection.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
15#[cfg_attr(feature = "defmt", derive(defmt::Format))]
16pub enum KeepAlive {
17    /// There is no keep alive mechanism. Any amount of time can pass between 2 MQTT packets
18    #[default]
19    Infinite,
20
21    /// The maximum time interval in seconds allowed to pass between 2 MQTT packets.
22    ///
23    /// Must be greater than 0.
24    Seconds(NonZero<u16>),
25}
26
27impl KeepAlive {
28    pub(crate) fn as_u16(self) -> u16 {
29        match self {
30            KeepAlive::Infinite => 0,
31            KeepAlive::Seconds(s) => s.get(),
32        }
33    }
34}
35
36/// The handling of a session after a disconnection.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
38#[cfg_attr(feature = "defmt", derive(defmt::Format))]
39pub enum SessionExpiryInterval {
40    /// The session ends the moment a DISCONNECT packet is sent or the network connection closes.
41    #[default]
42    EndOnDisconnect,
43    /// The session is not ended under any circumstances.
44    NeverEnd,
45    /// The session ends after this many seconds have passed after a DISCONNECT packet is sent or the network connection closes.
46    Seconds(u32),
47}
48
49/// Maximum packet size. Exceeding this is a protocol error.
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
51#[cfg_attr(feature = "defmt", derive(defmt::Format))]
52pub enum MaximumPacketSize {
53    /// There is no imposed limit on how large packets can be.
54    /// The technical limit is [`crate::types::VarByteInt::MAX_ENCODABLE`] + 5 (size of fixed header).
55    #[default]
56    Unlimited,
57
58    /// There is a limit on how large packets can be. The packet size is the value of its remaining length
59    /// plus the size of the fixed header. A value of 0 is not allowed by the specification. A value less
60    /// than 2 does not make sense because every MQTT packet contains a fixed header of at least 2 bytes.
61    Limit(NonZero<u32>),
62}
63
64impl MaximumPacketSize {
65    pub(crate) fn as_u32(self) -> u32 {
66        match self {
67            Self::Unlimited => u32::MAX,
68            Self::Limit(l) => l.get(),
69        }
70    }
71}
72
73/// Maximum concurrent publications with a Quality of Service > 0.
74///
75/// Default is 65536 / [`u16::MAX`] and is used when no receive maximum is present. Can't be zero.
76#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77#[cfg_attr(feature = "defmt", derive(defmt::Format))]
78pub struct ReceiveMaximum(pub NonZero<u16>);