-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconfig.rs
79 lines (72 loc) · 2.68 KB
/
config.rs
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
use crate::{ProtocolError, Will};
use core::str::FromStr;
use embedded_time::duration::Milliseconds;
use heapless::String;
/// Configuration specifying the operational state of the MQTT client.
pub struct Config<'a> {
pub(crate) rx_buffer: &'a mut [u8],
pub(crate) tx_buffer: &'a mut [u8],
pub(crate) state_buffer: &'a mut [u8],
pub(crate) client_id: String<64>,
pub(crate) keepalive_interval: Option<Milliseconds<u32>>,
pub(crate) downgrade_qos: bool,
pub(crate) will: Option<Will<'a>>,
}
impl<'a> Config<'a> {
/// Construct configuration for the MQTT client.
///
/// # Args
/// * `rx` - Memory used for receiving messages. The length of this buffer is the maximum
/// receive packet length.
/// * `tx` - Memory used for transmitting messages. The length of this buffer is the max
/// transmit length.
pub fn new(rx: &'a mut [u8], tx: &'a mut [u8]) -> Self {
Self {
rx_buffer: rx,
tx_buffer: tx,
state_buffer: &mut [],
client_id: String::new(),
keepalive_interval: None,
downgrade_qos: false,
will: None,
}
}
/// Provide additional buffer space if messages above [QoS::AtMostOnce] are required.
pub fn session_state(mut self, buffer: &'a mut [u8]) -> Self {
self.state_buffer = buffer;
self
}
/// Specify a known client ID to use. If not assigned, the broker will auto assign an ID.
pub fn client_id(mut self, id: &str) -> Result<Self, ProtocolError> {
self.client_id = String::from_str(id).or(Err(ProtocolError::ProvidedClientIdTooLong))?;
Ok(self)
}
/// Configure the MQTT keep-alive interval.
///
/// # Note
/// The broker may override the requested keep-alive interval. Any value requested by the
/// broker will be used instead.
///
/// # Args
/// * `interval` - The keep-alive interval in seconds. A ping will be transmitted if no other
/// messages are sent within 50% of the keep-alive interval.
pub fn keepalive_interval(mut self, seconds: u16) -> Self {
self.keepalive_interval
.replace(Milliseconds(seconds as u32 * 1000));
self
}
/// Specify if publication [QoS] should be automatically downgraded to the maximum supported by
/// the server if they exceed the server [QoS] maximum.
pub fn autodowngrade_qos(mut self) -> Self {
self.downgrade_qos = true;
self
}
/// Specify the Will message to be sent if the client disconnects.
///
/// # Args
/// * `will` - The will to use.
pub fn will(mut self, will: Will<'a>) -> Self {
self.will.replace(will);
self
}
}