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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#![cfg_attr(not(test), no_std)]
//! # MiniMQ
//! Provides a minimal MQTTv5 client and message parsing for the MQTT version 5 protocol.
//!
//! This crate provides a minimalistic MQTT 5 client that can be used to publish topics to an MQTT
//! broker and subscribe to receive messages on specific topics.
//!
//! # Limitations
//! This library does not currently support the following elements:
//! * Subscribing above Quality-of-service `AtMostOnce`
//! * Server Authentication
//! * Topic aliases
//!
//! # Requirements
//! This library requires that the user provide it an object that implements a basic TcpStack that
//! can be used as the transport layer for MQTT communications.
//!
//! The maximum message size is configured through generic parameters. This allows the maximum
//! message size to be configured by the user. Note that buffers will be allocated on the stack, so it
//! is important to select a size such that the stack does not overflow.
//!
//! # Example
//! Below is a sample snippet showing how this library is used.
//!
//! ```no_run
//! use minimq::{ConfigBuilder, Minimq, Publication};
//!
//! // Construct an MQTT client with a maximum packet size of 256 bytes
//! // and a maximum of 16 messages that are allowed to be "in flight".
//! // Messages are "in flight" if QoS::AtLeastOnce has not yet been acknowledged (PUBACK)
//! // or QoS::ExactlyOnce has not been completed (PUBCOMP).
//! // Connect to a broker at localhost - Use a client ID of "test".
//! let mut buffer = [0; 256];
//! let localhost: embedded_nal::IpAddr = "127.0.0.1".parse().unwrap();
//! let mut mqtt: Minimq<'_, _, _, minimq::broker::IpBroker> = Minimq::new(
//!         std_embedded_nal::Stack::default(),
//!         std_embedded_time::StandardClock::default(),
//!         ConfigBuilder::new(localhost.into(), &mut buffer)
//!             .client_id("test").unwrap(),
//!         );
//!
//! let mut subscribed = false;
//!
//! loop {
//!     if mqtt.client().is_connected() && !subscribed {
//!         mqtt.client().subscribe(&["topic".into()], &[]).unwrap();
//!         subscribed = true;
//!     }
//!
//!     // The client must be continually polled to update the MQTT state machine.
//!     mqtt.poll(|client, topic, message, properties| {
//!         match topic {
//!             "topic" => {
//!                println!("{:?}", message);
//!                let response = Publication::new(message).topic("echo").finish().unwrap();
//!                client.publish(response).unwrap();
//!             },
//!             topic => println!("Unknown topic: {}", topic),
//!         };
//!     }).unwrap();
//! }
//! ```

pub mod broker;
pub mod config;
mod de;
mod message_types;
pub mod mqtt_client;
mod network_manager;
mod packets;
mod properties;
pub mod publication;
mod reason_codes;
mod republication;
mod ring_buffer;
mod ser;
mod session_state;
pub mod types;
mod varint;
mod will;

pub use broker::Broker;
pub use config::ConfigBuilder;
pub use properties::Property;
pub use publication::{DeferredPublication, Publication};
pub use reason_codes::ReasonCode;
pub use will::Will;

pub use embedded_nal;
pub use embedded_time;
pub use mqtt_client::Minimq;
use num_enum::TryFromPrimitive;

pub use de::Error as DeError;
pub use ser::Error as SerError;

#[cfg(feature = "logging")]
pub(crate) use log::{debug, error, info, trace, warn};

/// Default port number for unencrypted MQTT traffic
///
/// # Note:
/// See [IANA Port Numbers](https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt)
pub const MQTT_INSECURE_DEFAULT_PORT: u16 = 1883;

/// Default port number for encrypted MQTT traffic
///
/// # Note:
/// See [IANA Port Numbers](https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt)
pub const MQTT_SECURE_DEFAULT_PORT: u16 = 8883;

/// The quality-of-service for an MQTT message.
#[derive(Debug, Copy, Clone, PartialEq, TryFromPrimitive, PartialOrd)]
#[repr(u8)]
pub enum QoS {
    /// A packet will be delivered at most once, but may not be delivered at all.
    AtMostOnce = 0,

    /// A packet will be delivered at least one time, but possibly more than once.
    AtLeastOnce = 1,

    /// A packet will be delivered exactly one time.
    ExactlyOnce = 2,
}

/// The retained status for an MQTT message.
#[derive(Debug, Copy, Clone, PartialEq, TryFromPrimitive)]
#[repr(u8)]
pub enum Retain {
    /// The message shall not be retained by the broker.
    NotRetained = 0,

    /// The message shall be marked for retention by the broker.
    Retained = 1,
}

/// Errors that are specific to the MQTT protocol implementation.
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ProtocolError {
    ProvidedClientIdTooLong,
    UnexpectedPacket,
    InvalidProperty,
    MalformedPacket,
    BufferSize,
    BadIdentifier,
    Unacknowledged,
    WrongQos,
    UnsupportedPacket,
    NoTopic,
    AuthAlreadySpecified,
    WillAlreadySpecified,
    Failed(ReasonCode),
    Serialization(SerError),
    Deserialization(DeError),
}

#[derive(Debug, PartialEq)]
pub enum PubError<T, E> {
    Error(Error<T>),
    Serialization(E),
}

impl<T, E> From<crate::ser::PubError<E>> for PubError<T, E> {
    fn from(e: crate::ser::PubError<E>) -> Self {
        match e {
            crate::ser::PubError::Other(e) => crate::PubError::Serialization(e),
            crate::ser::PubError::Error(e) => crate::PubError::Error(crate::Error::Minimq(
                crate::MinimqError::Protocol(ProtocolError::from(e)),
            )),
        }
    }
}

impl<T, E> From<Error<T>> for PubError<T, E> {
    fn from(e: Error<T>) -> Self {
        Self::Error(e)
    }
}

impl From<crate::ser::Error> for ProtocolError {
    fn from(err: crate::ser::Error) -> Self {
        ProtocolError::Serialization(err)
    }
}

impl From<crate::de::Error> for ProtocolError {
    fn from(err: crate::de::Error) -> Self {
        ProtocolError::Deserialization(err)
    }
}

impl From<ReasonCode> for ProtocolError {
    fn from(code: ReasonCode) -> Self {
        ProtocolError::Failed(code)
    }
}

#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum MinimqError {
    Protocol(ProtocolError),
    Clock(embedded_time::clock::Error),
}

/// Possible errors encountered during an MQTT connection.
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum Error<E> {
    WriteFail,
    NotReady,
    Unsupported,
    NoResponseTopic,
    SessionReset,
    Network(E),
    Minimq(MinimqError),
}

impl<E> From<MinimqError> for Error<E> {
    fn from(minimq: MinimqError) -> Self {
        Error::Minimq(minimq)
    }
}

impl From<embedded_time::clock::Error> for MinimqError {
    fn from(clock: embedded_time::clock::Error) -> Self {
        MinimqError::Clock(clock)
    }
}

impl<E> From<ProtocolError> for Error<E> {
    fn from(p: ProtocolError) -> Self {
        Error::Minimq(p.into())
    }
}

impl<E> From<embedded_time::clock::Error> for Error<E> {
    fn from(clock: embedded_time::clock::Error) -> Self {
        Error::Minimq(clock.into())
    }
}

impl From<ProtocolError> for MinimqError {
    fn from(error: ProtocolError) -> Self {
        MinimqError::Protocol(error)
    }
}

#[doc(hidden)]
#[cfg(not(feature = "logging"))]
mod mqtt_log {
    #[doc(hidden)]
    #[macro_export]
    macro_rules! trace {
        ($($arg:tt)+) => {
            ()
        };
    }

    #[doc(hidden)]
    #[macro_export]
    macro_rules! debug {
        ($($arg:tt)+) => {
            ()
        };
    }

    #[doc(hidden)]
    #[macro_export]
    macro_rules! info {
        ($($arg:tt)+) => {
            ()
        };
    }

    #[doc(hidden)]
    #[macro_export]
    macro_rules! warn {
        ($($arg:tt)+) => {
            ()
        };
    }

    #[doc(hidden)]
    #[macro_export]
    macro_rules! error {
        ($($arg:tt)+) => {
            ()
        };
    }
}