MQTT Encoding and Decoding: Difference between revisions

From IoT with AME
Jump to navigation Jump to search
No edit summary
No edit summary
Line 8: Line 8:


* Fixed header
* Fixed header
** b7-4: MQTT Packet Typ, b3-0: Flags
** packet type:byte
** Remaining length - this one is a bit tricky and consumes between 1 and 4 bytes. Encoding is least significant bits first in b6-0 while b7 is a "continuation bit" ('1' meaning "another byte follows").
*** b7-4: MQTT packet type
** remaining length - this one is a bit tricky and consumes between 1 and 4 bytes. Encoding is least significant bits first in b6-0 while b7 is a "continuation bit" ('1' meaning "another byte follows").
* Variable header (optional), depending on the package type
* Variable header (optional), depending on the package type
* Payload (optional), depending on the package type
* Payload (optional), depending on the package type


Text is encoded in UTF-8 without a terminating "0" byte, after the number of bytes in the text encoded as two bytes, MSB first.
Type "Array" is binary data, after the number of bytes in the text encoded as two bytes, MSB first.
 
Type "String" is like "Array", text encoded in UTF-8 without a terminating "0" byte.


{| class="wikitable" style="border: 2px solid"
{| class="wikitable" style="border: 2px solid"
Line 31: Line 34:
| 0
| 0
| -
| -
| "MQTT":String<br/>protocol level (4):byte<br/>connect flags:byte (b7: user, b6: password, b5: will retain, b4-3: will QoS, b2: will, b1: clean session)<br/>keep alive:word
|  
| client ID:String<br/>will topic:String (if present)<br/>will payload:Array (if present)<br/>user name:String (if present)<br/>password:Array (if present)
* "MQTT":String
* protocol level (4):byte
* connect flags:byte
** b7: user
** b6: password, b5: will retain
** b4-3: will QoS
** b2: will
** b1: clean session)
* keep alive:word
|
* client ID:String
* will topic:String (if present)
* will payload:Array (if present)
* user name:String (if present)
* password:Array (if present)
|-
|-
| CONNACK
| CONNACK
Line 39: Line 56:
| 0
| 0
| -
| -
| connect acknowledge flags:byte (b0: session present)<br/>connect return code:byte (0: connection accepted, 1: unacceptable  
|  
protocol version, 2: identifier rejected, 3: server unavailable, 4: bad user name or password, 5: not authorized)
* connect acknowledge flags:byte
** b0: session present
* connect return code:byte
** 0: connection accepted
** 1: unacceptable protocol version
** 2: identifier rejected
** 3: server unavailable
** 4: bad user name or password
** 5: not authorized
| -
| -
|-
|-
Line 46: Line 71:
| 3
| 3
| Both
| Both
| b3: DUP, b2-1: QoS, b0: RETAIN
|  
* b3: DUP
* b2-1: QoS
* b0: RETAIN
| QoS > 0 ? y
| QoS > 0 ? y
| topic name:String<br/>message ID:word
|
* topic name:String
* message ID:word
| payload:Array (optional)
| payload:Array (optional)
|-
|-
Line 56: Line 86:
| 0
| 0
| y
| y
| message ID of PUBLISH packet:word
|
* message ID of PUBLISH packet:word
| -
| -
|-
|-
Line 64: Line 95:
| 0
| 0
| y
| y
| message ID of PUBLISH packet:word
|  
* message ID of PUBLISH packet:word
| -
| -
|-
|-
Line 72: Line 104:
| 1
| 1
| y
| y
| message ID of PUBREC packet:word
|  
* message ID of PUBREC packet:word
| -
| -
|-
|-
Line 80: Line 113:
| 0
| 0
| y
| y
| message ID of PUBREL packet:word
|  
* message ID of PUBREL packet:word
| -
| -
|-
|-
Line 88: Line 122:
| 1
| 1
| y
| y
| message ID:word
|  
| topic filter:String<br/>requested QoS:byte (b1-0: QoS)<br/>(repeat sequence one or more times)
* message ID:word
|  
* topic filter:String
* requested QoS:byte (b1-0: QoS)
repeat sequence one or more times
|-
|-
| SUBACK
| SUBACK
Line 96: Line 134:
| 0
| 0
| y
| y
| message ID of SUBSCRIBE packet:word
|  
| return code:byte (b7: failure, b1-0: maximum QoS)<br/>(repeat sequence one or more times)
* message ID of SUBSCRIBE packet:word
|  
* return code:byte
** b7: failure
** b1-0: maximum QoS
repeat sequence one or more times
|-
|-
| UNSUBSCRIBE
| UNSUBSCRIBE
Line 104: Line 147:
| 1
| 1
| y
| y
| message ID:word
|  
| topic filter:String<br/>(repeat sequence one or more times)
* message ID:word
|
* topic filter:String
repeat sequence one or more times
|-
|-
| UNSUBACK
| UNSUBACK
Line 112: Line 158:
| 0
| 0
| y
| y
| message ID of SUBSCRIBE packet:word
|  
* message ID of SUBSCRIBE packet:word
| -
| -
|-
|-

Revision as of 08:37, 4 July 2017

MQTT

MQTT Encoding and Decoding

The MQTT protocol is very lightweight (does not use many bytes to get the message across).

Each MQTT packet consists of:

  • Fixed header
    • packet type:byte
      • b7-4: MQTT packet type
    • remaining length - this one is a bit tricky and consumes between 1 and 4 bytes. Encoding is least significant bits first in b6-0 while b7 is a "continuation bit" ('1' meaning "another byte follows").
  • Variable header (optional), depending on the package type
  • Payload (optional), depending on the package type

Type "Array" is binary data, after the number of bytes in the text encoded as two bytes, MSB first.

Type "String" is like "Array", text encoded in UTF-8 without a terminating "0" byte.

Type Number Direction Flags Variable Header Payload
Msg ID
CONNECT 1 Device to Broker 0 -
  • "MQTT":String
  • protocol level (4):byte
  • connect flags:byte
    • b7: user
    • b6: password, b5: will retain
    • b4-3: will QoS
    • b2: will
    • b1: clean session)
  • keep alive:word
  • client ID:String
  • will topic:String (if present)
  • will payload:Array (if present)
  • user name:String (if present)
  • password:Array (if present)
CONNACK 2 Broker to Device 0 -
  • connect acknowledge flags:byte
    • b0: session present
  • connect return code:byte
    • 0: connection accepted
    • 1: unacceptable protocol version
    • 2: identifier rejected
    • 3: server unavailable
    • 4: bad user name or password
    • 5: not authorized
-
PUBLISH 3 Both
  • b3: DUP
  • b2-1: QoS
  • b0: RETAIN
QoS > 0 ? y
  • topic name:String
  • message ID:word
payload:Array (optional)
PUBACK 4 Both 0 y
  • message ID of PUBLISH packet:word
-
PUBREC 5 Both 0 y
  • message ID of PUBLISH packet:word
-
PUBREL 6 Both 1 y
  • message ID of PUBREC packet:word
-
PUBCOMP 7 Both 0 y
  • message ID of PUBREL packet:word
-
SUBSCRIBE 8 Device to Broker 1 y
  • message ID:word
  • topic filter:String
  • requested QoS:byte (b1-0: QoS)

repeat sequence one or more times

SUBACK 9 Broker to Device 0 y
  • message ID of SUBSCRIBE packet:word
  • return code:byte
    • b7: failure
    • b1-0: maximum QoS

repeat sequence one or more times

UNSUBSCRIBE 10 Device to Broker 1 y
  • message ID:word
  • topic filter:String

repeat sequence one or more times

UNSUBACK 11 Broker to Device 0 y
  • message ID of SUBSCRIBE packet:word
-
PINGREQ 12 Device to Broker 0 - -
PINGRESP 13 Broker to Device 0 - -
DISCONNECT 14 Device to Broker 0 - -

MQTT