RustMqtt
Mqtt encoder and decoder for pure rust.
Notes
- The byte order for
mqttpackets isBig-Endian, means all the bytes will be arranged atMSB->LSB. - Strings/Text values are
UTF-8, the format is2-byte+n-bytes, where first2-byteare length of the string and anothern-bytesis the actual textual data. Max length of text field is65535bytes.
Control Packets
All the MQtt control packets consists of 3 parts.
Fixed header, present in all MQTT Control Packets.Variable header, present in some MQTT Control Packets.Payload, present in some MQTT Control Packets.
1. Fixed Header
- First byte in fixed header contains various settings as well as what type of packet this is.
7th 6th 5th 4th, these4 bitsare the type of packet.3rd 2nd 1st 0th, these4 bitsarepacket specific. mostly utilized forpublishpacket type.
- Second on wards bytes contains the actual remaining length of the whole packet including variable header and payload if any.
- The Remaining Length is encoded using a variable length encoding scheme which uses a single byte for values up to 127. Larger values are handled as follows. The least significant seven bits of each byte encode the data, and the most significant bit is used to indicate that there are following bytes in the representation. Thus each byte encodes 128 values and "continuation bit". The maximum number of bytes in the Remaining Length field is four.
-
do encodedByte = X MOD 128 X = X DIV 128 // if there are more data to encode, set the top bit of this byte if ( X > 0 ) encodedByte = encodedByte OR 128 endif 'output' encodedByte while ( X > 0 )
2. Variable Header
- Some types of MQTT Control Packets contain a variable header component. It resides between the fixed header and the payload. The content of the variable header varies depending on the Packet type. The Packet Identifier field of variable header is common in several packet types.
Packet Identifier- non-zero
2 byteslonger. - These Control Packets are PUBLISH (where QoS > 0), PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK.
- non-zero
3. Payload
- Some MQTT Control Packets contain a payload as the final part of the packet.
Control Packet Payload
CONNECT Required
CONNACK None
PUBLISH Optional
PUBACK None
PUBREC None
PUBREL None
PUBCOMP None
SUBSCRIBE Required
SUBACK Required
UNSUBSCRIBE Required
UNSUBACK None
PINGREQ None
PINGRESP None
DISCONNECT None
MQTT Control Packets
-
CONNECT
clientrequest for connection toserver.- After network connection is established from client to server, the first packet to send from client side MUST be this.
clientcan only send 1 CONNECT packet per network connection.Fixed Header7th 6th 5th 4th->0 0 0 13rd 2nd 1st 0th->0 0 0 0
Remaining LengthVariable HeaderProtocol Name- This is UTF-8 encoded string value.
- Possible Values:
MQTTfor 3.1.1
Protocol Level1 bytecontaining the version of mqtt.- for latest, it is
4.
Connect Flags1 bytecontains all the required connect flags.7th bit->username6th bit->password5th bit->will retain4th 3rd bits->Will QoS2nd bit->Will Flag1st bit->Clean Session0th bit->Reserved
Keep alive2 bytescontains information about keep alive.- This is time value in
seconds.
PayloadClient ID->UTF-8encoded unique string.Will Topic- if
will flagis1. - UTF-8 encoded string.
- if
Will Message- if
will flagis1. - UTF-8 encoded string.
- if
Username- if
username flagis1. - UTF-8 encoded string.
- if
Password- if
password flagis1. - UTF-8 encoded string.
- if
-
CONNACK
- Acknowledge connection request
Fixed Header- is
0 0 1 0 0 0 0 0.
- is
Remaining Length- is
0 0 0 0 0 0 1 0.
- is
Variable header1st byteis0 0 0 0 0 0 0 SP, whereSPisSession Present.2nd byteisReturn Code.-
0 0x00 Connection Accepted 1 0x01 Connection Refused, The Server does not support the level of the MQTT protocol requested by the Client 2 0x02 Connection Refused, identifier rejected The Client identifier is correct UTF-8 but not allowed by the Server 3 0x03 Connection Refused, Server unavailable 4 0x04 Connection Refused, bad user name or password 5 0x05 Connection Refused, not authorized 6 - 255 Reserved.
-
-
PUBLISH
- Packet is used when
clientwants to send data toserver, orserverwants to send data toclient. Fixed Header 1st byte7th 6th 5th 4th bits->0 0 1 1, This is the message type.3rd bit->DUP Flag,- if DUP = 0, this is first attempt to send message from client or server. if DUP = 1, then this might be re-attempt to send the message.
- for QoS = 0, DUP will always be 0.
2nd 1st buts->Qos Level- 0, 1, 2 values.
1st bit-> 'RETAIN Flag'- When this is 1, we will store this message and will publish to all new subscribers just after subscription is finished.
Remaining Length 2nd.. bytesVariable headerTopic Name-> sort of key of the message that we are trying to publish,UTF-8.PacketID->u16packet id to id the message.
Payload- The type of data in payload is application specific.
- If
Qos = 0forPUBLISHpacket, then there won't be any response from receiver to sender.
- Packet is used when
-
PUBACK
- This packet is sent from receiver to sender when
Qos = 1inPUBLISHpacket. Fixed Header0 1 0 0 0 0 0 0
Remaining Length0 0 0 0 0 0 1 0
Variable HeaderPacketId-> 2 bytes
- No Paylaod.
- This packet is sent from receiver to sender when
-
PUBREC
- When
Qos=2inPUBLISH, receiver will sendPUBRECpacket to sender. Fixed Header0 1 0 1 0 0 0 0
Remaining Length0 0 0 0 0 0 1 0
Variable headerPacketID-> This is the packet id we received from publish packet.
- no payload.
- When
-
PUBREL
- This is the response to
PUBRECpacket. sent from sender to receiver after sender receivesPUBRECfrom receiver. Fixed Header0 1 1 0 0 0 1 0
Remaing Length0 0 0 0 0 0 1 0
Variable HeaderPacketId-> same packet id, which was received at the time ofPUBREC.
- no payload.
- This is the response to
-
PUBCOMP
- This is the response to
PUBRELpacket. sent from receiver to sender after receiver receivesPUBRELfrom sender. Fixed Header0 1 1 1 0 0 1 0
Remaing Length0 0 0 0 0 0 1 0
Variable HeaderPacketId-> same packet id, which was received at the time ofPUBREL.
- no payload.
- This is the response to
-
SUBSCRIBE
- This packet is sent from
clienttoserverto createsubscription. - Each subscription registers client's interest for one or more
topics. - This is used when server wants to send
PUBLISHpackets to client. Subscribepacket also describeMAX QoSwith which server can send packets to client.Fixed Header1 0 0 0 0 0 1 0
Remaining Length 2nd.. bytesVariable HeaderPacketID-> unique u16 id to identify the packet.
Payload- List of
Topic FiltersplusQoS. Where topic filters must be UTF-8 encoded strings.
- List of
- This packet is sent from
-
SUBACK
- Sent from server to client to confirm the subscription for client on perticular topic filter.
- This contains list of return codes, that spacifies the max QoS was granted in each subscription.
Fixed Header1 0 0 1 0 0 0 0
Remaining Length- Length of variable header + payload.
Variable Header- Packet ID
Payload- The payload contains a list of return codes. Each return code corresponds to a Topic Filter in the SUBSCRIBE Packet being acknowledged.
Allowed return codes: 0x00 - Success - Maximum QoS 0 0x01 - Success - Maximum QoS 1 0x02 - Success - Maximum QoS 2 0x80 - Failure ```
-
UNSUBSCRIBE
- Sent from
clienttoserverto remove interest from one or more topics. Fixed Header1 0 1 0 0 0 1 0
Remaining LengthVariable Header- Packet ID
Payload- List of topic filters client want to unscribe to.
- Topics must be UTF-8 encoded strings.
- Sent from
-
UNSUBACK
- The UNSUBACK Packet is sent by the
Serverto theClientto confirm receipt of an UNSUBSCRIBE Packet. Fixed Header1 0 1 1 0 0 0 0
Remaining LengthVariable Header- Packet ID received from unsubscribe request.
- No Payload.
- The UNSUBACK Packet is sent by the
-
PINGREQ
- The PINGREQ Packet is sent from a Client to the Server.
- Indicate to the Server that the Client is alive in the absence of any other Control Packets being sent from the Client to the Server.
- Request that the Server responds to confirm that it is alive.
- Exercise the network to indicate that the Network Connection is active.
Fixed Header1 1 0 0 0 0 0 0
- remaining length is 0.
- Has no payload or variable headers.
-
PINGRESP
- A PINGRESP Packet is sent by the Server to the Client in response to a PINGREQ Packet. That indicates that server is alive.
Fixed Header1 1 0 1 0 0 0 0
- remaining length is 0.
- Has no payload or variable headers.
-
DISCONNECT
- The DISCONNECT Packet is the final Control Packet sent from the Client to the Server. This indicates that client is disconnecting cleanly.
Fixed Header1 1 1 0 0 0 0 0
- remaining length is 0.
- Has no payload or variable headers.
- After sending this packet, client must close the network connection, and not send any more control packets.
- server should close the client connection, if client failed to do so, and not accept any new packets from the client.