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
//! This is a low level (no_std) crate with the ability to assemble and disassemble MQTT 3.1.1
//! packets and is used by both client and broker. Uses 'bytes' crate internally

#![no_std]
use cfg_if::cfg_if;

extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "std")]
mod codec;
mod packetbytes;
mod packets;
mod read;
mod topic;
mod write;

#[cfg(feature = "std")]
pub use codec::*;
pub use packetbytes::*;
pub use packets::*;
pub use read::*;
pub use topic::*;
pub use write::*;

cfg_if! {
    if #[cfg(feature = "std")] {
        /// Serialization and deserialization errors
        #[derive(Debug, thiserror::Error)]
        pub enum Error {
            #[error("Invalid connect return code `{0}`")]
            InvalidConnectReturnCode(u8),
            #[error("Invalid protocol. Expecting 'MQTT' in payload")]
            InvalidProtocol,
            #[error("Invalid protocol level `{0}`")]
            InvalidProtocolLevel(u8),
            #[error("Incorrect packet format")]
            IncorrectPacketFormat,
            #[error("Unsupported Packet type `{0}`")]
            InvalidPacketType(u8),
            #[error("Unsupported QoS `{0}`")]
            InvalidQoS(u8),
            #[error("Invalid packet identifier = 0")]
            PacketIdZero,
            #[error("Payload size incorrect")]
            PayloadSizeIncorrect,
            #[error("Payload too long")]
            PayloadTooLong,
            #[error("Payload size limit exceeded")]
            PayloadSizeLimitExceeded,
            #[error("Payload required")]
            PayloadRequired,
            #[error("Topic name must only contain valid UTF-8")]
            TopicNotUtf8,
            #[error("Malformed remaining length")]
            MalformedRemainingLength,
            #[error("Trying to access wrong boundary")]
            BoundaryCrossed,
            #[error("EOF. Not enough data in buffer")]
            UnexpectedEof,
            #[error("I/O")]
            Io(#[from] std::io::Error),
        }
    } else {
        /// Serialization and deserialization errors
        pub enum Error {
            InvalidConnectReturnCode(u8),
            InvalidProtocol,
            InvalidProtocolLevel(u8),
            IncorrectPacketFormat,
            InvalidPacketType(u8),
            InvalidQoS(u8),
            PacketIdZero,
            PayloadSizeIncorrect,
            PayloadTooLong,
            PayloadSizeLimitExceeded,
            PayloadRequired,
            TopicNotUtf8,
            BoundaryCrossed,
            MalformedRemainingLength,
            UnexpectedEof,
        }
    }
}

/// MQTT packet type
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PacketType {
    Connect = 1,
    ConnAck,
    Publish,
    PubAck,
    PubRec,
    PubRel,
    PubComp,
    Subscribe,
    SubAck,
    Unsubscribe,
    UnsubAck,
    PingReq,
    PingResp,
    Disconnect,
}

/// Packet type from a byte
///
/// ```ignore
///          7                          3                          0
///          +--------------------------+--------------------------+
/// byte 1   | MQTT Control Packet Type | Flags for each type      |
///          +--------------------------+--------------------------+
///          |         Remaining Bytes Len  (1 - 4 bytes)          |
///          +-----------------------------------------------------+
///
/// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Figure_2.2_-
/// ```
pub fn packet_type(num: u8) -> Result<PacketType, Error> {
    match num {
        1 => Ok(PacketType::Connect),
        2 => Ok(PacketType::ConnAck),
        3 => Ok(PacketType::Publish),
        4 => Ok(PacketType::PubAck),
        5 => Ok(PacketType::PubRec),
        6 => Ok(PacketType::PubRel),
        7 => Ok(PacketType::PubComp),
        8 => Ok(PacketType::Subscribe),
        9 => Ok(PacketType::SubAck),
        10 => Ok(PacketType::Unsubscribe),
        11 => Ok(PacketType::UnsubAck),
        12 => Ok(PacketType::PingReq),
        13 => Ok(PacketType::PingResp),
        14 => Ok(PacketType::Disconnect),
        _ => Err(Error::InvalidPacketType(num)),
    }
}

/// Protocol type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Protocol {
    MQTT(u8),
}


/// Quality of service
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
pub enum QoS {
    AtMostOnce = 0,
    AtLeastOnce = 1,
    ExactlyOnce = 2,
}

struct FixedHeader {
    byte1: u8,
    header_len: usize,
    remaining_len: usize,
}

/// Maps a number to QoS
pub fn qos(num: u8) -> Result<QoS, Error> {
    match num {
        0 => Ok(QoS::AtMostOnce),
        1 => Ok(QoS::AtLeastOnce),
        2 => Ok(QoS::ExactlyOnce),
        qos => Err(Error::InvalidQoS(qos)),
    }
}