1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use core::num::NonZero;
use const_fn::const_fn;
use crate::{
client::options::WillOptions,
config::{KeepAlive, MaximumPacketSize, SessionExpiryInterval},
types::{MqttBinary, MqttString},
};
/// Options for a connection.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Options<'c> {
/// If set to true, a new session is started. If set to false, an existing session is continued.
pub clean_start: bool,
/// The setting of the keep alive the client wishes. Can be set to a different value by the server.
pub keep_alive: KeepAlive,
/// The setting of the session expiry interval the server wishes. Can be set to a different value by the server.
pub session_expiry_interval: SessionExpiryInterval,
/// The maximum packet size that the client is willing to accept
pub maximum_packet_size: MaximumPacketSize,
/// When set to true, the broker may return response information used to construct response topics.
pub request_response_information: bool,
/// The user name the client wishes to authenticate with.
pub user_name: Option<MqttString<'c>>,
/// The password the client wishes to perform basic authentication with.
pub password: Option<MqttBinary<'c>>,
/// The will configuration for the session of the connection.
pub will: Option<WillOptions<'c>>,
}
impl Default for Options<'_> {
fn default() -> Self {
Self::new()
}
}
impl<'c> Options<'c> {
/// Creates new connect options with no clean start, infinite keep alive and session
/// expiry immediately after disconnecting.
#[must_use]
pub const fn new() -> Self {
Self {
clean_start: false,
keep_alive: KeepAlive::Infinite,
session_expiry_interval: SessionExpiryInterval::EndOnDisconnect,
maximum_packet_size: MaximumPacketSize::Unlimited,
request_response_information: false,
user_name: None,
password: None,
will: None,
}
}
/// Sets the clean start flag to true.
#[must_use]
pub const fn clean_start(mut self) -> Self {
self.clean_start = true;
self
}
/// Sets the desired keep alive of the connection.
#[must_use]
pub const fn keep_alive(mut self, keep_alive: KeepAlive) -> Self {
self.keep_alive = keep_alive;
self
}
/// Sets the desired session expiry interval of the connection.
#[must_use]
pub const fn session_expiry_interval(mut self, interval: SessionExpiryInterval) -> Self {
self.session_expiry_interval = interval;
self
}
/// Sets the maximum packet size as a limit in bytes. A value less than 2 does not make sense.
#[must_use]
pub const fn maximum_packet_size(mut self, maximum_packet_size: NonZero<u32>) -> Self {
self.maximum_packet_size = MaximumPacketSize::Limit(maximum_packet_size);
self
}
/// Sets the request response information property to true prompting the server to return
/// a response information property to construct response topics.
#[must_use]
pub const fn request_response_information(mut self) -> Self {
self.request_response_information = true;
self
}
/// Sets the user name.
#[const_fn(cfg(not(feature = "alloc")))]
#[must_use]
pub const fn user_name(mut self, user_name: MqttString<'c>) -> Self {
self.user_name = Some(user_name);
self
}
/// Sets the password.
#[const_fn(cfg(not(feature = "alloc")))]
#[must_use]
pub const fn password(mut self, password: MqttBinary<'c>) -> Self {
self.password = Some(password);
self
}
/// Sets the will.
#[const_fn(cfg(not(feature = "alloc")))]
#[must_use]
pub const fn will(mut self, will: WillOptions<'c>) -> Self {
self.will = Some(will);
self
}
}